Skip to content

Commit 72ab406

Browse files
committedApr 27, 2020
refactor: merge the log util
1 parent a32a67e commit 72ab406

File tree

1 file changed

+56
-59
lines changed

1 file changed

+56
-59
lines changed
 

‎lib/log.js

+56-59
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,29 @@
33
const { Console } = require('console');
44
const chalk = require('chalk');
55

6-
const LEVEL = {
7-
trace: 10,
8-
debug: 20,
9-
info: 30,
10-
warn: 40,
11-
error: 50,
12-
fatal: 60
13-
};
6+
const TRACE = 10;
7+
const DEBUG = 20;
8+
const INFO = 30;
9+
const WARN = 40;
10+
const ERROR = 50;
11+
const FATAL = 60;
1412

1513
const LEVEL_NAMES = {
16-
trace: 'TRACE',
17-
debug: 'DEBUG',
18-
info: 'INFO ',
19-
warn: 'WARN ',
20-
error: 'ERROR',
21-
fatal: 'FATAL'
14+
10: 'TRACE',
15+
20: 'DEBUG',
16+
30: 'INFO ',
17+
40: 'WARN ',
18+
50: 'ERROR',
19+
60: 'FATAL'
2220
};
2321

2422
const LEVEL_COLORS = {
25-
trace: 'gray',
26-
debug: 'gray',
27-
info: 'green',
28-
warn: 'bgYellow',
29-
error: 'bgRed',
30-
fatal: 'bgRed'
23+
10: 'gray',
24+
20: 'gray',
25+
30: 'green',
26+
40: 'bgYellow',
27+
50: 'bgRed',
28+
60: 'bgRed'
3129
};
3230

3331
const console = new Console({
@@ -41,73 +39,72 @@ class Logger {
4139
const silent = options.silent || false;
4240
this._debug = options.debug || false;
4341

44-
this.level = LEVEL.info;
42+
this.level = INFO;
4543

4644
if (silent) {
47-
this.level = LEVEL.warn;
45+
this.level = WARN;
4846
}
4947

5048
if (this._debug) {
51-
this.level = LEVEL.trace;
49+
this.level = TRACE;
5250
}
5351
}
5452

55-
_writeDebugTimestamp(stdType = 'stdout') {
53+
_writeLogOutput(level, consoleArgs) {
5654
if (this._debug) {
57-
const str = new Date().toISOString().substring(11, 23);
58-
process[stdType].write(chalk[LEVEL_COLORS.debug](str + ' '));
55+
const str = new Date().toISOString().substring(11, 23) + ' ';
56+
57+
if (level === TRACE || level >= WARN) {
58+
process.stderr.write(chalk[LEVEL_COLORS[DEBUG]](str));
59+
} else {
60+
process.stdout.write(chalk[LEVEL_COLORS[DEBUG]](str));
61+
}
62+
}
63+
64+
if (level >= this.level) {
65+
const str = chalk[LEVEL_COLORS[level]](LEVEL_NAMES[level]) + ' ';
66+
if (level === TRACE || level >= WARN) {
67+
process.stderr.write(str);
68+
} else {
69+
process.stdout.write(str);
70+
}
71+
72+
if (level === TRACE) {
73+
console.trace(...consoleArgs);
74+
} else if (level < INFO) {
75+
console.debug(...consoleArgs);
76+
} else if (level < WARN) {
77+
console.info(...consoleArgs);
78+
} else if (level < ERROR) {
79+
console.warn(...consoleArgs);
80+
} else {
81+
console.error(...consoleArgs);
82+
}
5983
}
6084
}
6185

6286
trace(...args) {
63-
if (LEVEL.trace >= this.level) {
64-
this._writeDebugTimestamp('stderr');
65-
process.stderr.write(chalk[LEVEL_COLORS.trace](LEVEL_NAMES.trace) + ' ');
66-
console.trace(...args);
67-
}
87+
this._writeLogOutput(TRACE, args);
6888
}
6989

7090
debug(...args) {
71-
if (LEVEL.debug >= this.level) {
72-
this._writeDebugTimestamp();
73-
process.stdout.write(chalk[LEVEL_COLORS.debug](LEVEL_NAMES.debug) + ' ');
74-
// For Node.js 10 and higher, console.debug is an alias for console.log
75-
console.debug(...args);
76-
}
91+
this._writeLogOutput(DEBUG, args);
7792
}
7893

7994
info(...args) {
80-
if (LEVEL.info >= this.level) {
81-
this._writeDebugTimestamp();
82-
process.stdout.write(chalk[LEVEL_COLORS.info](LEVEL_NAMES.info) + ' ');
83-
// For Node.js 10 and higher, console.info is an alias for console.log
84-
console.info(...args);
85-
}
95+
this._writeLogOutput(INFO, args);
8696
}
8797

8898
warn(...args) {
89-
if (LEVEL.warn >= this.level) {
90-
this._writeDebugTimestamp('stderr');
91-
process.stderr.write(chalk[LEVEL_COLORS.warn](LEVEL_NAMES.warn) + ' ');
92-
// For Node.js 10 and higher, console.info is an alias for console.error
93-
console.warn(...args);
94-
}
99+
this._writeLogOutput(WARN, args);
95100
}
96101

97102
error(...args) {
98-
if (LEVEL.error >= this.level) {
99-
this._writeDebugTimestamp('stderr');
100-
process.stderr.write(chalk[LEVEL_COLORS.error](LEVEL_NAMES.error) + ' ');
101-
console.error(...args);
102-
}
103+
this._writeLogOutput(ERROR, args);
103104
}
104105

105106
fatal(...args) {
106-
if (LEVEL.fatal >= this.level) {
107-
this._writeDebugTimestamp('stderr');
108-
process.stderr.write(chalk[LEVEL_COLORS.fatal](LEVEL_NAMES.fatal) + ' ');
109-
console.error(...args);
110-
}
107+
this._writeLogOutput(FATAL, args);
111108
}
112109
}
113110

0 commit comments

Comments
 (0)
Please sign in to comment.