@youpaichris/logger 5.0.0 → 6.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/index.js +24 -2
  2. package/package.json +1 -1
  3. package/tests.js +21 -0
package/index.js CHANGED
@@ -5,6 +5,14 @@
5
5
  * Chris / QQ:10512
6
6
  */
7
7
 
8
+ const fs = require('fs');
9
+ const path = require('path');
10
+
11
+
12
+ // const logFilePath = path.join(__dirname, `${path.basename(path.dirname(__filename))}.log`);
13
+ const logFilePath = path.join(`${path.basename(path.dirname(module.parent ? module.parent.filename : __filename))}.log`);
14
+ const logFile = fs.createWriteStream(logFilePath, {flags: 'a'});
15
+
8
16
  const __log = console.log;
9
17
  const __log__ = function () {
10
18
  if (arguments[3] === '\x1B[41m') {
@@ -117,8 +125,9 @@ function getCallerPath() {
117
125
  }
118
126
 
119
127
  class Logger {
120
- constructor(path) {
128
+ constructor({path = null, save = false}) {
121
129
  this.path = path ? path : getCallerPath();
130
+ this.save = save;
122
131
  }
123
132
 
124
133
  info(...msg) {
@@ -130,6 +139,7 @@ class Logger {
130
139
  ...msg,
131
140
  consoleStyles['blue'][1] + consoleStyles['bold'][1] + '\b'
132
141
  );
142
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | INFO | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
133
143
  };
134
144
 
135
145
  warn(...msg) {
@@ -141,6 +151,7 @@ class Logger {
141
151
  ...msg,
142
152
  consoleStyles['yellow'][1] + consoleStyles['bold'][1] + '\b'
143
153
  );
154
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | WARNING | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
144
155
  };
145
156
 
146
157
  error(...msg) {
@@ -152,6 +163,7 @@ class Logger {
152
163
  ...msg,
153
164
  consoleStyles['red'][1] + consoleStyles['bold'][1] + '\b'
154
165
  );
166
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | ERROR | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
155
167
  };
156
168
 
157
169
  success(...msg) {
@@ -163,6 +175,8 @@ class Logger {
163
175
  ...msg,
164
176
  consoleStyles['green'][1] + consoleStyles['bold'][1] + '\b'
165
177
  );
178
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | SUCCESS | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
179
+
166
180
  };
167
181
 
168
182
  debug(...msg) {
@@ -174,6 +188,8 @@ class Logger {
174
188
  ...msg,
175
189
  consoleStyles['magenta'][1] + consoleStyles['bold'][1] + '\b'
176
190
  );
191
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | DEBUG | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
192
+
177
193
  };
178
194
 
179
195
  call(...msg) {
@@ -185,6 +201,8 @@ class Logger {
185
201
  ...msg,
186
202
  consoleStyles['cyan'][1] + consoleStyles['bold'][1] + '\b'
187
203
  );
204
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | CALL | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
205
+
188
206
  };
189
207
 
190
208
  critical(...msg) {
@@ -196,6 +214,8 @@ class Logger {
196
214
  ...msg
197
215
  // consoleStyles['redBG'][1] + consoleStyles['bold'][1] + '\b'
198
216
  );
217
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | CRITICAL| ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
218
+
199
219
  };
200
220
 
201
221
 
@@ -217,7 +237,9 @@ class Logger {
217
237
  logger.success("Hello World!");
218
238
  logger.call("Hello World!");
219
239
  */
220
-
240
+ process.on('exit', () => {
241
+ logFile.end();
242
+ });
221
243
  module.exports = Logger;
222
244
 
223
245
  /*
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@youpaichris/logger",
3
- "version": "5.0.0",
3
+ "version": "6.0.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/tests.js ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @File : tests.js
3
+ * @Time : 2024/7/18 14:57
4
+ * @Author : Chris
5
+ * @Email : 10512@qq.com
6
+ * @Software : WebStorm
7
+ * @Platform : macOS
8
+ * */
9
+ const Logger = require('./index.js');
10
+
11
+ const logger = new Logger({
12
+ save: true,
13
+ });
14
+
15
+ logger.info("info")
16
+ logger.debug("debug");
17
+ logger.error("error");
18
+ logger.warn("warn");
19
+ logger.success("success");
20
+ logger.call("call");
21
+ logger.critical("critical");