@youpaichris/logger 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.idea/Logger.iml +12 -0
- package/.idea/jsLibraryMappings.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/index.js +231 -0
- package/package.json +11 -0
package/.idea/Logger.iml
ADDED
@@ -0,0 +1,12 @@
|
|
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>
|
package/index.js
ADDED
@@ -0,0 +1,231 @@
|
|
1
|
+
/*!
|
2
|
+
* JavaScript logger
|
3
|
+
* Logger - v1.0.0 (2021-01-15T14:55:51+0800)
|
4
|
+
* Chris / QQ:10512
|
5
|
+
*/
|
6
|
+
|
7
|
+
const __warn = console.warn;
|
8
|
+
console.warn = function () {
|
9
|
+
if (arguments[3] === '\x1B[41m') {
|
10
|
+
arguments[4] = "\b" + arguments[4];
|
11
|
+
arguments[arguments.length-1] += consoleStyles['redBG'][1] + consoleStyles['bold'][1] + '\b';
|
12
|
+
}
|
13
|
+
return __warn.apply(this, arguments)
|
14
|
+
}
|
15
|
+
const loggerConfig = {
|
16
|
+
Date: (() => {
|
17
|
+
return Date
|
18
|
+
})(),
|
19
|
+
warn: (() => {
|
20
|
+
return console.warn
|
21
|
+
})(),
|
22
|
+
Error: (() => {
|
23
|
+
return Error
|
24
|
+
})(),
|
25
|
+
RegExp: (() => {
|
26
|
+
return RegExp
|
27
|
+
})(),
|
28
|
+
|
29
|
+
}
|
30
|
+
|
31
|
+
const _error = console.error;
|
32
|
+
console.error = (...msg) => {
|
33
|
+
let isOutput = true;
|
34
|
+
[...msg].forEach(v => {
|
35
|
+
if (v?.includes?.(`Setting the NODE_TLS_REJECT_UNAUTHORIZED`)) {
|
36
|
+
isOutput = false;
|
37
|
+
}
|
38
|
+
})
|
39
|
+
isOutput && _error(...msg);
|
40
|
+
}
|
41
|
+
Object.defineProperty(global, '__stack', {
|
42
|
+
get: function () {
|
43
|
+
return new Error().stack.split('\n').slice(2);
|
44
|
+
}
|
45
|
+
});
|
46
|
+
Object.defineProperty(global, '__line', {
|
47
|
+
get: function () {
|
48
|
+
const stack = new Error().stack.split('\n').slice(2);
|
49
|
+
const caller = stack[1].trim();
|
50
|
+
const index = caller.indexOf('at ') + 3;
|
51
|
+
const clean = caller.slice(index);
|
52
|
+
const parts = clean.split(':');
|
53
|
+
return parts[parts.length - 2];
|
54
|
+
}
|
55
|
+
});
|
56
|
+
|
57
|
+
function dateFormat(fmt, date) {
|
58
|
+
let ret;
|
59
|
+
const opt = {
|
60
|
+
"Y+": date.getFullYear().toString(),
|
61
|
+
"m+": (date.getMonth() + 1).toString(),
|
62
|
+
"d+": date.getDate().toString(),
|
63
|
+
"H+": date.getHours().toString(),
|
64
|
+
"M+": date.getMinutes().toString(),
|
65
|
+
"S+": date.getSeconds().toString(),
|
66
|
+
"ms+": (date.getMilliseconds().toString()).padStart(3, "0"),
|
67
|
+
};
|
68
|
+
for (let k in opt) {
|
69
|
+
ret = new loggerConfig.RegExp("(" + k + ")").exec(fmt);
|
70
|
+
if (ret) {
|
71
|
+
fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
|
72
|
+
}
|
73
|
+
}
|
74
|
+
return fmt;
|
75
|
+
}
|
76
|
+
|
77
|
+
const consoleStyles = {
|
78
|
+
'bold': ['\x1B[1m', '\x1B[22m'],
|
79
|
+
'italic': ['\x1B[3m', '\x1B[23m'],
|
80
|
+
'underline': ['\x1B[4m', '\x1B[24m'],
|
81
|
+
'inverse': ['\x1B[7m', '\x1B[27m'],
|
82
|
+
'strikethrough': ['\x1B[9m', '\x1B[29m'],
|
83
|
+
'white': ['\x1B[37m', '\x1B[39m'],
|
84
|
+
'grey': ['\x1B[90m', '\x1B[39m'],
|
85
|
+
'blue': ['\x1B[34m', '\x1B[39m'],
|
86
|
+
'cyan': ['\x1B[36m', '\x1B[39m'],
|
87
|
+
'green': ['\x1B[32m', '\x1B[39m'],
|
88
|
+
'magenta': ['\x1B[35m', '\x1B[39m'],
|
89
|
+
'red': ['\x1B[31m', '\x1B[39m'],
|
90
|
+
'yellow': ['\x1B[33m', '\x1B[39m'],
|
91
|
+
'whiteBG': ['\x1B[47m', '\x1B[49m'],
|
92
|
+
'greyBG': ['\x1B[49;5;8m', '\x1B[49m'],
|
93
|
+
'blueBG': ['\x1B[40m', '\x1B[49m'],
|
94
|
+
'cyanBG': ['\x1B[46m', '\x1B[49m'],
|
95
|
+
'greenBG': ['\x1B[42m', '\x1B[49m'],
|
96
|
+
'magentaBG': ['\x1B[45m', '\x1B[49m'],
|
97
|
+
'redBG': ['\x1B[41m', '\x1B[49m'],
|
98
|
+
'yellowBG': ['\x1B[43m', '\x1B[49m']
|
99
|
+
}
|
100
|
+
|
101
|
+
function getCallerPath() {
|
102
|
+
const stack = new Error().stack.split('\n');
|
103
|
+
if (stack.length >= 4) {
|
104
|
+
const callerLine = stack[3];
|
105
|
+
let callerPath = callerLine.trim().substring(callerLine.indexOf('(') + 1, callerLine.lastIndexOf(':'));
|
106
|
+
callerPath = callerPath.replaceAll("\\", "/");
|
107
|
+
const regex = /\/([^\/]+:\d+:\d+)/;
|
108
|
+
const match = callerPath.match(regex);
|
109
|
+
if (match && match[1]) {
|
110
|
+
const fileNameWithLine = match[1];
|
111
|
+
return fileNameWithLine.split(':')[0];
|
112
|
+
}
|
113
|
+
}
|
114
|
+
return "Anonymous";
|
115
|
+
}
|
116
|
+
|
117
|
+
class Logger {
|
118
|
+
constructor(path) {
|
119
|
+
this.path = path ? path : getCallerPath();
|
120
|
+
}
|
121
|
+
|
122
|
+
info(...msg) {
|
123
|
+
let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
|
124
|
+
loggerConfig.warn(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
|
125
|
+
consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
|
126
|
+
consoleStyles['bold'][0] + consoleStyles['blue'][0] + "INFO " + consoleStyles['red'][0] + " |" +
|
127
|
+
consoleStyles['blue'][0], ` ${pathLine.padEnd(22, " ")}${consoleStyles['blue'][0]}📡`, consoleStyles['blue'][0],
|
128
|
+
...msg,
|
129
|
+
consoleStyles['blue'][1] + consoleStyles['bold'][1] + '\b'
|
130
|
+
);
|
131
|
+
};
|
132
|
+
|
133
|
+
warn(...msg) {
|
134
|
+
let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
|
135
|
+
loggerConfig.warn(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
|
136
|
+
consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
|
137
|
+
consoleStyles['bold'][0] + consoleStyles['yellow'][0] + "WARNING " + consoleStyles['red'][0] + " |" +
|
138
|
+
consoleStyles['blue'][0], ` ${pathLine.padEnd(22, " ")}${consoleStyles['yellow'][0]}💡`, consoleStyles['yellow'][0],
|
139
|
+
...msg,
|
140
|
+
consoleStyles['yellow'][1] + consoleStyles['bold'][1] + '\b'
|
141
|
+
);
|
142
|
+
};
|
143
|
+
|
144
|
+
error(...msg) {
|
145
|
+
let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
|
146
|
+
loggerConfig.warn(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
|
147
|
+
consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
|
148
|
+
consoleStyles['bold'][0] + consoleStyles['red'][0] + "ERROR " + consoleStyles['red'][0] + " |" +
|
149
|
+
consoleStyles['blue'][0], ` ${pathLine.padEnd(22, " ")}${consoleStyles['red'][0]}🚨`, consoleStyles['red'][0],
|
150
|
+
...msg,
|
151
|
+
consoleStyles['red'][1] + consoleStyles['bold'][1] + '\b'
|
152
|
+
);
|
153
|
+
};
|
154
|
+
|
155
|
+
success(...msg) {
|
156
|
+
let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
|
157
|
+
loggerConfig.warn(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
|
158
|
+
consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
|
159
|
+
consoleStyles['bold'][0] + consoleStyles['green'][0] + "SUCCESS " + consoleStyles['red'][0] + " |" +
|
160
|
+
consoleStyles['blue'][0], ` ${pathLine.padEnd(22, " ")}${consoleStyles['green'][0]}🎯`, consoleStyles['green'][0],
|
161
|
+
...msg,
|
162
|
+
consoleStyles['green'][1] + consoleStyles['bold'][1] + '\b'
|
163
|
+
);
|
164
|
+
};
|
165
|
+
|
166
|
+
debug(...msg) {
|
167
|
+
let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
|
168
|
+
loggerConfig.warn(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
|
169
|
+
consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
|
170
|
+
consoleStyles['bold'][0] + consoleStyles['magenta'][0] + "DEBUG " + consoleStyles['red'][0] + " |" +
|
171
|
+
consoleStyles['blue'][0], ` ${pathLine.padEnd(22, " ")}${consoleStyles['magenta'][0]}🚧`, consoleStyles['magenta'][0],
|
172
|
+
...msg,
|
173
|
+
consoleStyles['magenta'][1] + consoleStyles['bold'][1] + '\b'
|
174
|
+
);
|
175
|
+
};
|
176
|
+
|
177
|
+
call(...msg) {
|
178
|
+
let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
|
179
|
+
loggerConfig.warn(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
|
180
|
+
consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
|
181
|
+
consoleStyles['bold'][0] + consoleStyles['cyan'][0] + "CALL " + consoleStyles['red'][0] + " |" +
|
182
|
+
consoleStyles['blue'][0], ` ${pathLine.padEnd(22, " ")}${consoleStyles['cyan'][0]}🔔`, consoleStyles['cyan'][0],
|
183
|
+
...msg,
|
184
|
+
consoleStyles['cyan'][1] + consoleStyles['bold'][1] + '\b'
|
185
|
+
);
|
186
|
+
};
|
187
|
+
|
188
|
+
critical(...msg) {
|
189
|
+
let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
|
190
|
+
loggerConfig.warn(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
|
191
|
+
consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
|
192
|
+
consoleStyles['bold'][0] + consoleStyles['redBG'][0] + consoleStyles['white'][0] + "CRITICAL" + consoleStyles['redBG'][1] + consoleStyles['red'][0] + " |" +
|
193
|
+
consoleStyles['blue'][0], ` ${pathLine.padEnd(22, " ")}${consoleStyles['cyan'][0]}🔴 ${consoleStyles['white'][0]}`, consoleStyles['redBG'][0],
|
194
|
+
...msg
|
195
|
+
// consoleStyles['redBG'][1] + consoleStyles['bold'][1] + '\b'
|
196
|
+
);
|
197
|
+
};
|
198
|
+
|
199
|
+
|
200
|
+
}
|
201
|
+
|
202
|
+
// export default Logger;
|
203
|
+
/* es6:
|
204
|
+
How to use:
|
205
|
+
import Logger from "./Logger.js";
|
206
|
+
import path from "path";
|
207
|
+
import {fileURLToPath} from 'url';
|
208
|
+
|
209
|
+
const __filename = fileURLToPath(import.meta.url)
|
210
|
+
const logger = new Logger(path.basename(__filename));
|
211
|
+
logger.info("Hello World!");
|
212
|
+
logger.debug("Hello World!");
|
213
|
+
logger.warn("Hello World!");
|
214
|
+
logger.error("Hello World!");
|
215
|
+
logger.success("Hello World!");
|
216
|
+
logger.call("Hello World!");
|
217
|
+
*/
|
218
|
+
|
219
|
+
module.exports = Logger;
|
220
|
+
|
221
|
+
/* How to use:
|
222
|
+
const logger = new Logger();
|
223
|
+
logger.info(`info`);
|
224
|
+
logger.debug("debug")
|
225
|
+
logger.warn("warn")
|
226
|
+
logger.call("call")
|
227
|
+
logger.error("error")
|
228
|
+
logger.critical("critical.", 2, 3, "4")
|
229
|
+
logger.info("info again");
|
230
|
+
*/
|
231
|
+
|