forked from AlekEagle/sharex-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
83 lines (81 loc) · 2.26 KB
/
logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
let chalk = require("chalk");
let _console = { ...require('console') };
let date = function getNowDateTimeStr() {
let now = new Date();
let hour = now.getHours();
return [
[
AddZero(now.getDate()),
AddZero(now.getMonth() + 1),
now.getFullYear(),
].join("/"),
[AddZero(hour), AddZero(now.getMinutes()), AddZero(now.getSeconds())].join(
":"
),
].join(" ");
};
const logLevels = [
'NONE',
'ERROR',
'WARN',
'LOG',
'INFO',
'DEBUG'
]
function AddZero(num) {
return num >= 0 && num < 10 ? "0" + num : num + "";
}
class Logger {
constructor(logLevel) {
this.logLevel = typeof logLevel === 'string' ? logLevels.indexOf(logLevel) !== -1 ? logLevel : 'LOG' : logLevels[logLevel];
this.logLevels = [...logLevels];
}
log(thing, ...args) {
if (logLevels.indexOf(this.logLevel) < logLevels.indexOf('LOG')) return;
_console.log(
`${chalk.blue.inverse(date())} ${chalk.inverse(`[LOG]`)} ${chalk.white(
thing
)}`,
!args.length ? "" : args.join(' ')
);
}
warn(thing, ...args) {
if (logLevels.indexOf(this.logLevel) < logLevels.indexOf('WARN')) return;
_console.log(
`${chalk.blue.inverse(date())} ${chalk
.rgb(255, 124, 43)
.inverse(`[WARN]`)} ${chalk.white(thing)}`,
!args.length ? "" : args.join(' ')
);
}
debug(thing, ...args) {
if (logLevels.indexOf(this.logLevel) < logLevels.indexOf('DEBUG')) return;
_console.log(
`${chalk.blue.inverse(date())} ${chalk.grey.inverse(
`[DEBUG]`
)} ${chalk.white(thing)}`,
!args.length ? "" : args.join(' ')
);
}
info(thing, ...args) {
if (logLevels.indexOf(this.logLevel) < logLevels.indexOf('INFO')) return;
_console.log(
`${chalk.blue.inverse(date())} ${chalk
.rgb(255, 255, 0)
.inverse(`[INFO]`)} ${chalk.white(thing)}`,
!args.length ? "" : args.join(' ')
);
}
error(thing, ...args) {
if (logLevels.indexOf(this.logLevel) < logLevels.indexOf('ERROR')) return;
_console.log(
`${chalk.blue.inverse(date())} ${chalk.bgRgb(
255,
0,
0
)(`[ERROR]`)} ${chalk.white(thing)}`,
!args.length ? "" : args.join(' ')
);
}
} Logger
module.exports = (logLevel) => global.console = new Logger(logLevel);