-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.ts
41 lines (37 loc) · 1.02 KB
/
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
import { createLogger, format, transports } from 'winston';
const { timestamp, errors, splat, json, printf, combine, colorize } = format;
const myFormat = printf(({ level, timestamp, message, ...rest }) => {
const timestampString = typeof timestamp === 'string' ? timestamp : '';
const messageString = typeof message === 'string' ? message : '';
return `[${timestampString}] ${level}: ${messageString}\n${
Object.keys(rest).length > 0 ? JSON.stringify(rest, null, 2) : ''
}`;
});
const defaultFormat = combine(
timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
errors({ stack: true }),
splat(),
json(),
myFormat
);
const logger = createLogger({
level: 'debug',
format: defaultFormat,
transports: [
new transports.File({
filename: 'error.log',
level: 'error',
dirname: 'logs'
}),
new transports.File({
filename: 'combined.log',
dirname: 'logs'
}),
new transports.Console({
format: combine(colorize(), defaultFormat)
})
]
});
export default logger;