@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.
- package/index.js +11 -7
- package/package.json +1 -1
- package/tests.js +1 -3
- 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
|
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(
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
160
|
+
criticalFilePath = path + "/logs/" + criticalFilePath;
|
161
|
+
criticalFile = createWriteStreamWithDirectories(criticalFilePath);
|
158
162
|
}
|
159
163
|
|
160
164
|
info(...msg) {
|
package/package.json
CHANGED
package/tests.js
CHANGED
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
|
+
}
|