@youpaichris/logger 6.0.6 → 6.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. package/index.js +11 -7
  2. package/package.json +1 -1
  3. package/tests.js +1 -3
  4. package/utils.js +20 -0
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  const fs = require('fs');
9
- const path = require('path');
9
+ const {createWriteStreamWithDirectories} = require('./utils.js');
10
10
 
11
11
  let logFilePath, logFile, successFilePath, successFile, errorFilePath, errorFile, criticalFilePath, criticalFile;
12
12
 
@@ -124,7 +124,7 @@ function getCallerPath() {
124
124
  class Logger {
125
125
  #prefix;
126
126
 
127
- constructor({path = null, save = false}) {
127
+ constructor(save = false, path = null) {
128
128
  this.path = path ? path : getCallerPath();
129
129
  this.save = save;
130
130
  if (save) {
@@ -143,18 +143,22 @@ class Logger {
143
143
  initLogPath() {
144
144
 
145
145
  let e = process.platform === `win32` ? __filename.split("\\") : __filename.split("/");
146
-
146
+ let path = e.slice(0, e.indexOf("node_modules")).join("/");
147
147
  logFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `log.log` : e.at(e.indexOf("node_modules") - 1) + `.log`;
148
- logFile = fs.createWriteStream(logFilePath, {flags: 'a'});
148
+ logFilePath = path + "/logs/" + logFilePath;
149
+ logFile = createWriteStreamWithDirectories(logFilePath);
149
150
 
150
151
  successFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `success.log` : e.at(e.indexOf("node_modules") - 1) + `-success.log`;
151
- successFile = fs.createWriteStream(successFilePath, {flags: 'a'});
152
+ successFilePath = path + "/logs/" + successFilePath;
153
+ successFile = createWriteStreamWithDirectories(successFilePath);
152
154
 
153
155
  errorFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `error.log` : e.at(e.indexOf("node_modules") - 1) + `-error.log`;
154
- errorFile = fs.createWriteStream(errorFilePath, {flags: 'a'});
156
+ errorFilePath = path + "/logs/" + errorFilePath;
157
+ errorFile = createWriteStreamWithDirectories(errorFilePath,);
155
158
 
156
159
  criticalFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `critical.log` : e.at(e.indexOf("node_modules") - 1) + `-critical.log`;
157
- criticalFile = fs.createWriteStream(criticalFilePath, {flags: 'a'});
160
+ criticalFilePath = path + "/logs/" + criticalFilePath;
161
+ criticalFile = createWriteStreamWithDirectories(criticalFilePath);
158
162
  }
159
163
 
160
164
  info(...msg) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@youpaichris/logger",
3
- "version": "6.0.6",
3
+ "version": "6.0.8",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/tests.js CHANGED
@@ -8,9 +8,7 @@
8
8
  * */
9
9
  const Logger = require('./index.js');
10
10
 
11
- const logger = new Logger({
12
- save: true,
13
- });
11
+ const logger = new Logger(true);
14
12
 
15
13
  logger.info("info")
16
14
  logger.debug("debug");
package/utils.js ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @File : utils.js
3
+ * @Time : 2024/7/19 11:35
4
+ * @Author : Chris
5
+ * @Email : 10512@qq.com
6
+ * @Software : WebStorm
7
+ * @Platform : macOS
8
+ * */
9
+ const fs = require('fs');
10
+ const path = require('path');
11
+
12
+ function createWriteStreamWithDirectories(logFilePath, options = {}) {
13
+ const dirPath = path.dirname(logFilePath);
14
+ fs.mkdirSync(dirPath, {recursive: true});
15
+ return fs.createWriteStream(logFilePath, {flags: 'a', ...options});
16
+ }
17
+
18
+ module.exports = {
19
+ createWriteStreamWithDirectories
20
+ }