1+ import * as util from 'util' ;
2+
13import { DonewsLoggerLevels , LoggerLevel } from '../constant' ;
2- import { LoggerInterface , LoggerOptions } from '../interfaces' ;
4+ import { LoggerInterface , LoggerOptions , LogInfoType } from '../interfaces' ;
35import { CommonUtil } from '../util/common.util' ;
46
57export class JsonLoggerService implements LoggerInterface {
@@ -21,19 +23,36 @@ export class JsonLoggerService implements LoggerInterface {
2123 return / ^ { | ( ^ \[ .* ] $ ) / gi. test ( str ) ;
2224 }
2325
26+ /**
27+ * 根据 log level 和 context 判断是否需要输出日志
28+ * @param {LoggerLevel }level
29+ * @param {string }context
30+ */
2431 isPrint ( level : LoggerLevel , context ?: string ) : boolean {
2532 if ( CommonUtil . checkContextByRegex ( this . loggerContextRegexList , context ) ) {
2633 return true ;
2734 }
2835 return DonewsLoggerLevels [ this . loggerLevel ] >= DonewsLoggerLevels [ level ] ;
2936 }
3037
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+
3251 // 防止message字段答应出json结构,导致es解析失败
3352 return JsonLoggerService . isJson ( message ) ? '\\' + message : message ;
3453 }
3554
36- error ( message : string , trace ?: string , context ?: string ) : void {
55+ error ( message : LogInfoType , context ?: string , trace ?: LogInfoType ) : void {
3756 if ( ! this . isPrint ( 'error' , context ) ) {
3857 return ;
3958 }
@@ -46,7 +65,7 @@ export class JsonLoggerService implements LoggerInterface {
4665 } ) ;
4766 }
4867
49- warn ( message : string , context ?: string ) : void {
68+ warn ( message : LogInfoType , context ?: string ) : void {
5069 if ( ! this . isPrint ( 'warn' , context ) ) {
5170 return ;
5271 }
@@ -57,7 +76,7 @@ export class JsonLoggerService implements LoggerInterface {
5776 } ) ;
5877 }
5978
60- log ( message : string , context ?: string ) : void {
79+ log ( message : LogInfoType , context ?: string ) : void {
6180 if ( ! this . isPrint ( 'log' , context ) ) {
6281 return ;
6382 }
@@ -68,7 +87,7 @@ export class JsonLoggerService implements LoggerInterface {
6887 } ) ;
6988 }
7089
71- info ( message : string , context ?: string ) : void {
90+ info ( message : LogInfoType , context ?: string ) : void {
7291 if ( ! this . isPrint ( 'info' , context ) ) {
7392 return ;
7493 }
@@ -79,7 +98,7 @@ export class JsonLoggerService implements LoggerInterface {
7998 } ) ;
8099 }
81100
82- debug ( message : string , context ?: string ) : void {
101+ debug ( message : LogInfoType , context ?: string ) : void {
83102 if ( ! this . isPrint ( 'debug' , context ) ) {
84103 return ;
85104 }
@@ -90,7 +109,7 @@ export class JsonLoggerService implements LoggerInterface {
90109 } ) ;
91110 }
92111
93- verbose ( message : string , context ?: string ) : void {
112+ verbose ( message : LogInfoType , context ?: string ) : void {
94113 if ( ! this . isPrint ( 'verbose' , context ) ) {
95114 return ;
96115 }
0 commit comments