-
Notifications
You must be signed in to change notification settings - Fork 0
/
winston-logger.ts
54 lines (49 loc) · 1.54 KB
/
winston-logger.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 { WinstonModule } from 'nest-winston';
import * as winston from 'winston';
import { createLogger, format } from 'winston';
import LokiTransport from 'winston-loki';
const { combine, timestamp, printf, colorize, errors } = format;
import { ConfigService } from '../config/config/config.service';
export class WinstonLogger {
static getInstance(config: ConfigService) {
const transports: winston.transport[] = [
new winston.transports.Console({
format: combine(
errors({ stack: true }),
timestamp(),
colorize({ all: true }),
printf(({ level, message, timestamp, context, stack }) => {
return `${timestamp} [${context || 'Application'}] ${level}: ${message} ${stack || ''}`;
}),
),
}),
];
if (config.env.LOKI_HOST) {
transports.push(
new LokiTransport({
format: combine(
errors({ stack: true }),
timestamp(),
printf(({ message, context, stack }) => {
return `[${context || 'Application'}]: ${message} ${stack || ''}`;
}),
),
host: config.env.LOKI_HOST,
labels: {
job: config.env.APP_NAME,
env: config.env.NODE_ENV,
},
onConnectionError: (err) => console.error(err),
}),
);
}
// createLogger of Winston
const instance = createLogger({
level: config.env.LOG_LEVEL || 'info',
transports,
});
return WinstonModule.createLogger({
instance,
});
}
}