-
Notifications
You must be signed in to change notification settings - Fork 11
/
WinstonLogger.service.ts
65 lines (60 loc) · 1.65 KB
/
WinstonLogger.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
55
56
57
58
59
60
61
62
63
64
65
import { LoggerService } from '@nestjs/common';
import * as winston from 'winston';
export class WinstonLoggerService implements LoggerService {
private readonly winstonLogger: any;
/**
* Creates an instance of WinstonLoggerService.
* @param {string} projectName
* @param {*} [customFormatter]
* @param {any[]} [transports]
* @memberof WinstonLoggerService
*/
constructor(projectName: string, customFormatter?: any, transports?: any[]) {
const formatter = customFormatter
? customFormatter
: this.getDefaultFormat();
this.winstonLogger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.simple(),
formatter,
),
defaultMeta: { service: projectName },
transports: [new winston.transports.Console(), ...(transports || [])],
});
}
private getDefaultFormat() {
const colorizer = winston.format.colorize();
return winston.format.printf(
({ level, message, timestamp, context, trace }) => {
return colorizer.colorize(
level,
`${timestamp} ${context || ''}: ${message} ${trace || ''}`,
);
},
);
}
public log(message: any, context?: string | undefined) {
this.winstonLogger.log({
level: 'info',
message,
context,
});
}
public error(message: any, trace?: string, context?: string) {
this.winstonLogger.log({
level: 'error',
message,
trace,
context,
});
}
public warn(message: any, context?: string) {
this.winstonLogger.log({
level: 'warn',
message,
context,
});
}
}