-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app-logger.service.ts
54 lines (45 loc) · 1.64 KB
/
app-logger.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Inject, Injectable, LoggerService } from '@nestjs/common';
import { pino, LoggerOptions } from 'pino';
import { AsyncLocalStorage } from 'async_hooks';
import { ASYNC_STORAGE } from 'src/global/constants';
import { ConfigService } from '@nestjs/config';
import { AppEnv } from 'src/services/app-config/configuration';
@Injectable()
export class AppLoggerService implements LoggerService {
private pino: pino.Logger;
constructor(
@Inject(ASYNC_STORAGE)
private readonly asyncStorage: AsyncLocalStorage<Map<string, string>>,
private readonly configService: ConfigService,
) {
const logLevel = configService.get('logLevel');
const appEnv = configService.get<AppEnv>('appEnv');
const loggerConfig: LoggerOptions = {
level: logLevel,
};
if (appEnv === AppEnv.DEV) {
loggerConfig.transport = {
target: 'pino-pretty',
};
}
this.pino = pino(loggerConfig);
}
error(message: any, trace?: string, context?: string): any {
const traceId = this.asyncStorage.getStore()?.get('traceId');
this.pino.error({ traceId }, this.getMessage(message, context));
if (trace) {
this.pino.error(trace);
}
}
log(message: any, context?: string): any {
const traceId = this.asyncStorage.getStore()?.get('traceId');
this.pino.info({ traceId }, this.getMessage(message, context));
}
warn(message: any, context?: string): any {
const traceId = this.asyncStorage.getStore()?.get('traceId');
this.pino.warn({ traceId }, this.getMessage(message, context));
}
private getMessage(message: any, context?: string) {
return context ? `[${context}] ${message}` : message;
}
}