@youpaichris/logger 4.0.0 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -5,6 +5,13 @@
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 logFile = fs.createWriteStream(logFilePath, {flags: 'a'});
14
+
8
15
  const __log = console.log;
9
16
  const __log__ = function () {
10
17
  if (arguments[3] === '\x1B[41m') {
@@ -117,8 +124,9 @@ function getCallerPath() {
117
124
  }
118
125
 
119
126
  class Logger {
120
- constructor(path) {
127
+ constructor({path = null, save = false}) {
121
128
  this.path = path ? path : getCallerPath();
129
+ this.save = save;
122
130
  }
123
131
 
124
132
  info(...msg) {
@@ -130,6 +138,7 @@ class Logger {
130
138
  ...msg,
131
139
  consoleStyles['blue'][1] + consoleStyles['bold'][1] + '\b'
132
140
  );
141
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | INFO | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
133
142
  };
134
143
 
135
144
  warn(...msg) {
@@ -141,6 +150,7 @@ class Logger {
141
150
  ...msg,
142
151
  consoleStyles['yellow'][1] + consoleStyles['bold'][1] + '\b'
143
152
  );
153
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | WARNING | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
144
154
  };
145
155
 
146
156
  error(...msg) {
@@ -152,6 +162,7 @@ class Logger {
152
162
  ...msg,
153
163
  consoleStyles['red'][1] + consoleStyles['bold'][1] + '\b'
154
164
  );
165
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | ERROR | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
155
166
  };
156
167
 
157
168
  success(...msg) {
@@ -163,6 +174,8 @@ class Logger {
163
174
  ...msg,
164
175
  consoleStyles['green'][1] + consoleStyles['bold'][1] + '\b'
165
176
  );
177
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | SUCCESS | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
178
+
166
179
  };
167
180
 
168
181
  debug(...msg) {
@@ -174,6 +187,8 @@ class Logger {
174
187
  ...msg,
175
188
  consoleStyles['magenta'][1] + consoleStyles['bold'][1] + '\b'
176
189
  );
190
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | DEBUG | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
191
+
177
192
  };
178
193
 
179
194
  call(...msg) {
@@ -185,6 +200,8 @@ class Logger {
185
200
  ...msg,
186
201
  consoleStyles['cyan'][1] + consoleStyles['bold'][1] + '\b'
187
202
  );
203
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | CALL | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
204
+
188
205
  };
189
206
 
190
207
  critical(...msg) {
@@ -196,6 +213,8 @@ class Logger {
196
213
  ...msg
197
214
  // consoleStyles['redBG'][1] + consoleStyles['bold'][1] + '\b'
198
215
  );
216
+ this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | CRITICAL| ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
217
+
199
218
  };
200
219
 
201
220
 
@@ -217,7 +236,9 @@ class Logger {
217
236
  logger.success("Hello World!");
218
237
  logger.call("Hello World!");
219
238
  */
220
-
239
+ process.on('exit', () => {
240
+ logFile.end();
241
+ });
221
242
  module.exports = Logger;
222
243
 
223
244
  /*
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@youpaichris/logger",
3
- "version": "4.0.0",
3
+ "version": "6.0.0",
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");
package/.idea/Logger.iml DELETED
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
6
- <excludeFolder url="file://$MODULE_DIR$/temp" />
7
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
- </content>
9
- <orderEntry type="inheritedJdk" />
10
- <orderEntry type="sourceFolder" forTests="false" />
11
- </component>
12
- </module>
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="JavaScriptLibraryMappings">
4
- <includedPredefinedLibrary name="Node.js Core" />
5
- </component>
6
- </project>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/Logger.iml" filepath="$PROJECT_DIR$/.idea/Logger.iml" />
6
- </modules>
7
- </component>
8
- </project>