3
3
const { Console } = require ( 'console' ) ;
4
4
const chalk = require ( 'chalk' ) ;
5
5
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 ;
14
12
15
13
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'
22
20
} ;
23
21
24
22
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'
31
29
} ;
32
30
33
31
const console = new Console ( {
@@ -41,73 +39,72 @@ class Logger {
41
39
const silent = options . silent || false ;
42
40
this . _debug = options . debug || false ;
43
41
44
- this . level = LEVEL . info ;
42
+ this . level = INFO ;
45
43
46
44
if ( silent ) {
47
- this . level = LEVEL . warn ;
45
+ this . level = WARN ;
48
46
}
49
47
50
48
if ( this . _debug ) {
51
- this . level = LEVEL . trace ;
49
+ this . level = TRACE ;
52
50
}
53
51
}
54
52
55
- _writeDebugTimestamp ( stdType = 'stdout' ) {
53
+ _writeLogOutput ( level , consoleArgs ) {
56
54
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
+ }
59
83
}
60
84
}
61
85
62
86
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 ) ;
68
88
}
69
89
70
90
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 ) ;
77
92
}
78
93
79
94
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 ) ;
86
96
}
87
97
88
98
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 ) ;
95
100
}
96
101
97
102
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 ) ;
103
104
}
104
105
105
106
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 ) ;
111
108
}
112
109
}
113
110
0 commit comments