1
+ import * as util from 'util' ;
2
+
1
3
import { DonewsLoggerLevels , LoggerLevel } from '../constant' ;
2
- import { LoggerInterface , LoggerOptions } from '../interfaces' ;
4
+ import { LoggerInterface , LoggerOptions , LogInfoType } from '../interfaces' ;
3
5
import { CommonUtil } from '../util/common.util' ;
4
6
5
7
export class JsonLoggerService implements LoggerInterface {
@@ -21,19 +23,36 @@ export class JsonLoggerService implements LoggerInterface {
21
23
return / ^ { | ( ^ \[ .* ] $ ) / gi. test ( str ) ;
22
24
}
23
25
26
+ /**
27
+ * 根据 log level 和 context 判断是否需要输出日志
28
+ * @param {LoggerLevel }level
29
+ * @param {string }context
30
+ */
24
31
isPrint ( level : LoggerLevel , context ?: string ) : boolean {
25
32
if ( CommonUtil . checkContextByRegex ( this . loggerContextRegexList , context ) ) {
26
33
return true ;
27
34
}
28
35
return DonewsLoggerLevels [ this . loggerLevel ] >= DonewsLoggerLevels [ level ] ;
29
36
}
30
37
31
- private static prepare ( message : string ) : string {
38
+ private static prepare ( value : LogInfoType ) : string {
39
+ let message = '' ;
40
+ if ( typeof value === 'object' ) {
41
+ try {
42
+ message = JSON . stringify ( value ) ;
43
+ } catch ( err ) {
44
+ message = util . inspect ( value ) ;
45
+ }
46
+ }
47
+ if ( typeof value === 'string' ) {
48
+ message = value ;
49
+ }
50
+
32
51
// 防止message字段答应出json结构,导致es解析失败
33
52
return JsonLoggerService . isJson ( message ) ? '\\' + message : message ;
34
53
}
35
54
36
- error ( message : string , trace ?: string , context ?: string ) : void {
55
+ error ( message : LogInfoType , context ?: string , trace ?: LogInfoType ) : void {
37
56
if ( ! this . isPrint ( 'error' , context ) ) {
38
57
return ;
39
58
}
@@ -46,7 +65,7 @@ export class JsonLoggerService implements LoggerInterface {
46
65
} ) ;
47
66
}
48
67
49
- warn ( message : string , context ?: string ) : void {
68
+ warn ( message : LogInfoType , context ?: string ) : void {
50
69
if ( ! this . isPrint ( 'warn' , context ) ) {
51
70
return ;
52
71
}
@@ -57,7 +76,7 @@ export class JsonLoggerService implements LoggerInterface {
57
76
} ) ;
58
77
}
59
78
60
- log ( message : string , context ?: string ) : void {
79
+ log ( message : LogInfoType , context ?: string ) : void {
61
80
if ( ! this . isPrint ( 'log' , context ) ) {
62
81
return ;
63
82
}
@@ -68,7 +87,7 @@ export class JsonLoggerService implements LoggerInterface {
68
87
} ) ;
69
88
}
70
89
71
- info ( message : string , context ?: string ) : void {
90
+ info ( message : LogInfoType , context ?: string ) : void {
72
91
if ( ! this . isPrint ( 'info' , context ) ) {
73
92
return ;
74
93
}
@@ -79,7 +98,7 @@ export class JsonLoggerService implements LoggerInterface {
79
98
} ) ;
80
99
}
81
100
82
- debug ( message : string , context ?: string ) : void {
101
+ debug ( message : LogInfoType , context ?: string ) : void {
83
102
if ( ! this . isPrint ( 'debug' , context ) ) {
84
103
return ;
85
104
}
@@ -90,7 +109,7 @@ export class JsonLoggerService implements LoggerInterface {
90
109
} ) ;
91
110
}
92
111
93
- verbose ( message : string , context ?: string ) : void {
112
+ verbose ( message : LogInfoType , context ?: string ) : void {
94
113
if ( ! this . isPrint ( 'verbose' , context ) ) {
95
114
return ;
96
115
}
0 commit comments