@youpaichris/logger 6.0.7 → 6.0.9

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 +12 -8
  2. package/package.json +1 -1
  3. package/tests.js +1 -1
  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
 
@@ -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) {
@@ -255,8 +259,8 @@ class Logger {
255
259
  consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
256
260
  consoleStyles['bold'][0] + consoleStyles['redBG'][0] + consoleStyles['white'][0] + "CRITICAL" + consoleStyles['redBG'][1] + consoleStyles['red'][0] + " |" +
257
261
  consoleStyles['blue'][0], ` ${pathLine.padEnd(22, " ")}${consoleStyles['cyan'][0]}🔴 ${consoleStyles['white'][0]}`, consoleStyles['redBG'][0],
258
- ...msg
259
- // consoleStyles['redBG'][1] + consoleStyles['bold'][1] + '\b'
262
+ ...msg,
263
+ consoleStyles['redBG'][1] + consoleStyles['bold'][1] + '\b', consoleStyles['redBG'][1] + consoleStyles['bold'][1] + '\b'
260
264
  );
261
265
  if (this.save) {
262
266
  let _msg = `${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | CRITICAL| ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@youpaichris/logger",
3
- "version": "6.0.7",
3
+ "version": "6.0.9",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/tests.js CHANGED
@@ -8,7 +8,7 @@
8
8
  * */
9
9
  const Logger = require('./index.js');
10
10
 
11
- const logger = new Logger();
11
+ const logger = new Logger(true);
12
12
 
13
13
  logger.info("info")
14
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
+ }