1
1
import { inspect } from 'util'
2
- import { isError , isString } from './is'
3
-
4
- export enum LoggerLevel {
5
- TRACE = 'TRACE' ,
6
- DEBUG = 'DEBUG' ,
7
- INFO = 'INFO' ,
8
- WARN = 'WARN' ,
9
- ERROR = 'ERROR' ,
10
- NONE = 'NONE' ,
2
+
3
+ export const enum LoggerLevel {
4
+ TRACE ,
5
+ DEBUG ,
6
+ INFO ,
7
+ WARN ,
8
+ ERROR ,
9
+ NONE ,
10
+ }
11
+
12
+ const displayLevel = {
13
+ [ LoggerLevel . TRACE ] : 'TRACE' ,
14
+ [ LoggerLevel . DEBUG ] : 'DEBUG' ,
15
+ [ LoggerLevel . INFO ] : 'INFO' ,
16
+ [ LoggerLevel . WARN ] : 'WARN' ,
17
+ [ LoggerLevel . ERROR ] : 'ERROR' ,
18
+ [ LoggerLevel . NONE ] : 'NONE' ,
19
+ }
20
+
21
+ function isString ( value : any ) : value is string {
22
+ return typeof value === 'string'
23
+ }
24
+
25
+ function isError ( value : any ) : value is Error {
26
+ return value instanceof Error
11
27
}
12
28
13
29
export class Logger {
14
- static Level = LoggerLevel
15
30
static options = {
16
31
enabled : new Set ( [ '*' ] ) ,
17
- level : LoggerLevel . TRACE ,
32
+ level : LoggerLevel . DEBUG ,
18
33
}
19
34
20
- constructor ( public readonly name : string , public readonly defaultContext : string = '' ) { }
35
+ constructor ( public readonly name : string , public readonly defaultContext : string = '' ) { }
21
36
22
37
trace ( msg : string , ...args : any [ ] ) : void
23
38
trace ( context : string , msg : string , ...args : any [ ] ) : void
@@ -28,37 +43,40 @@ export class Logger {
28
43
debug ( msg : string , ...args : any [ ] ) : void
29
44
debug ( context : string , msg : string , ...args : any [ ] ) : void
30
45
debug ( ...args : any [ ] ) {
31
- this . write ( LoggerLevel . TRACE , args )
46
+ this . write ( LoggerLevel . DEBUG , args )
32
47
}
33
48
34
49
info ( msg : string , ...args : any [ ] ) : void
35
50
info ( context : string , msg : string , ...args : any [ ] ) : void
36
51
info ( ...args : any [ ] ) {
37
- this . write ( LoggerLevel . TRACE , args )
52
+ this . write ( LoggerLevel . INFO , args )
38
53
}
39
54
40
55
warn ( msg : string , ...args : any [ ] ) : void
41
56
warn ( context : string , msg : string , ...args : any [ ] ) : void
42
57
warn ( ...args : any [ ] ) {
43
- this . write ( LoggerLevel . TRACE , args )
58
+ this . write ( LoggerLevel . WARN , args )
44
59
}
45
60
46
61
error ( msg : string , ...args : any [ ] ) : void
47
62
error ( msg : Error , ...args : any [ ] ) : void
48
63
error ( context : string , msg : string , ...args : any [ ] ) : void
49
64
error ( context : string , msg : Error , ...args : any [ ] ) : void
50
65
error ( ...args : any [ ] ) {
51
- this . write ( LoggerLevel . TRACE , args )
66
+ this . write ( LoggerLevel . ERROR , args )
52
67
}
53
68
54
69
private write ( level : LoggerLevel , args : any [ ] ) {
55
- if ( level >= Logger . options . level && ( Logger . options . enabled . has ( '*' ) || Logger . options . enabled . has ( this . name ) ) ) {
70
+ if (
71
+ level >= Logger . options . level &&
72
+ ( Logger . options . enabled . has ( '*' ) || Logger . options . enabled . has ( this . name ) )
73
+ ) {
56
74
const context =
57
75
args . length >= 2 && isString ( args [ 0 ] ) && ( isString ( args [ 1 ] ) || isError ( args [ 1 ] ) )
58
76
? args . shift ( )
59
77
: this . defaultContext
60
78
61
- const message = `${ Date . now ( ) } ${ level } [${ this . name } ]${ context ? ' (' + context + ')' : '' } ${ this . inspect (
79
+ const message = `${ Date . now ( ) } ${ displayLevel [ level ] } [${ this . name } ]${ context ? ' (' + context + ')' : '' } ${ this . inspect (
62
80
args ,
63
81
) } `
64
82
@@ -80,3 +98,5 @@ export class Logger {
80
98
return args . map ( ( arg ) => ( typeof arg === 'object' && arg ? inspect ( arg , true , null ) : arg ) ) . join ( ' ' )
81
99
}
82
100
}
101
+
102
+ export class DevLogger extends Logger { }
0 commit comments