Skip to content

Commit 1133133

Browse files
author
李太新
committed
feat: context 也校验 是否会json 字符串
1 parent 3e41476 commit 1133133

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lib/services/json-logger.service.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ export class JsonLoggerService implements LoggerInterface {
3939
}
4040
const formatted = JsonLoggerService.prepare(message);
4141
const formattedTrace = JsonLoggerService.prepare(trace);
42+
const formattedContext = JsonLoggerService.prepare(context);
4243
this.callFunction('error', formatted, {
4344
trace: formattedTrace,
44-
context: context || this.context,
45+
context: formattedContext || this.context,
4546
});
4647
}
4748

@@ -50,8 +51,9 @@ export class JsonLoggerService implements LoggerInterface {
5051
return;
5152
}
5253
const formatted = JsonLoggerService.prepare(message);
54+
const formattedContext = JsonLoggerService.prepare(context);
5355
this.callFunction('warn', formatted, {
54-
context: context || this.context,
56+
context: formattedContext || this.context,
5557
});
5658
}
5759

@@ -60,8 +62,9 @@ export class JsonLoggerService implements LoggerInterface {
6062
return;
6163
}
6264
const formatted = JsonLoggerService.prepare(message);
65+
const formattedContext = JsonLoggerService.prepare(context);
6366
this.callFunction('log', formatted, {
64-
context: context || this.context,
67+
context: formattedContext || this.context,
6568
});
6669
}
6770

@@ -70,8 +73,9 @@ export class JsonLoggerService implements LoggerInterface {
7073
return;
7174
}
7275
const formatted = JsonLoggerService.prepare(message);
76+
const formattedContext = JsonLoggerService.prepare(context);
7377
this.callFunction('info', formatted, {
74-
context: context || this.context,
78+
context: formattedContext || this.context,
7579
});
7680
}
7781

@@ -80,8 +84,9 @@ export class JsonLoggerService implements LoggerInterface {
8084
return;
8185
}
8286
const formatted = JsonLoggerService.prepare(message);
87+
const formattedContext = JsonLoggerService.prepare(context);
8388
this.callFunction('debug', formatted, {
84-
context: context || this.context,
89+
context: formattedContext || this.context,
8590
});
8691
}
8792

@@ -90,8 +95,9 @@ export class JsonLoggerService implements LoggerInterface {
9095
return;
9196
}
9297
const formatted = JsonLoggerService.prepare(message);
98+
const formattedContext = JsonLoggerService.prepare(context);
9399
this.callFunction('verbose', formatted, {
94-
context: context || this.context,
100+
context: formattedContext || this.context,
95101
});
96102
}
97103

test/json-logger.service.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ describe('json-logger.service.spec', () => {
3333
log.info('info', 'info');
3434
log.debug('debug', 'debug');
3535
log.verbose('verbose', 'verbose');
36+
37+
log.verbose(JSON.stringify({ test: 1 }), JSON.stringify({ test: 1 }));
3638
});
3739

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

0 commit comments

Comments
 (0)