Skip to content

Commit 4152da8

Browse files
author
李太新
committed
feat: json 日志调整 为 接受 json object
1 parent c4589e6 commit 4152da8

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

packages/nestjs-logger/lib/interfaces/logger-interface.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { ModuleMetadata } from '@nestjs/common/interfaces';
33

44
import { LOGGER_TYPE, LoggerLevel } from '../constant';
55

6+
export type LogInfoType = string | Record<any, any>;
7+
68
export interface LoggerInterface extends LoggerService {
7-
info(message: string, context?: string): void;
8-
error(message: string, trace?: string, context?: string): void;
9-
warn(message: string, context?: string): any;
10-
debug?(message: string, context?: string): any;
11-
verbose?(message: string, context?: string): any;
9+
info(message: LogInfoType, context?: string): void;
10+
error(message: LogInfoType, trace?: string, context?: string): void;
11+
warn(message: LogInfoType, context?: string): any;
12+
debug?(message: LogInfoType, context?: string): any;
13+
verbose?(message: LogInfoType, context?: string): any;
1214
setLogLevel(logLevel: LoggerLevel): void;
1315
setLogContextRegex(contextList: string | RegExp | (string | RegExp)[]): void;
1416
}

packages/nestjs-logger/lib/services/json-logger.service.ts

+27-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as util from 'util';
2+
13
import { DonewsLoggerLevels, LoggerLevel } from '../constant';
2-
import { LoggerInterface, LoggerOptions } from '../interfaces';
4+
import { LoggerInterface, LoggerOptions, LogInfoType } from '../interfaces';
35
import { CommonUtil } from '../util/common.util';
46

57
export 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
}

packages/nestjs-logger/test/json-logger.service.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ describe('json-logger.service.spec', () => {
3636
log.verbose('verbose', 'verbose');
3737

3838
log.verbose(JSON.stringify({ test: 1 }), JSON.stringify({ test: 1 }));
39+
log.verbose({ test: 1 }, JSON.stringify({ test: 1 }));
40+
log.verbose([{ test: 1 }], 'context');
3941
});
4042

4143
it('setLogContextRegex test', () => {

0 commit comments

Comments
 (0)