-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
55 lines (51 loc) · 1.48 KB
/
logger.js
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
const logger = require('winston');
const { format, transports } = logger;
require('winston-daily-rotate-file');
const fileLogOptions = {
// winston-daily-rotate-file options
datePattern: 'YYYY-MM-DD', // daily logs
handleExceptions: true,
maxFiles: '30d',
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.json()
)
}
// code from: https://github.com/winstonjs/winston
const mainLoggers = logger.createLogger({
level: 'info',
format: format.combine(
format.metadata({ fillExcept: ['timestamp', 'level', 'message', 'stack'] })
),
transports: [
new transports.DailyRotateFile({
...fileLogOptions,
level: 'debug',
// winston-daily-rotate-file options
filename: 'logs/combined-%DATE%.log',
}),
new transports.DailyRotateFile({
...fileLogOptions,
level: 'error',
// winston-daily-rotate-file options
filename: 'logs/error-%DATE%.log',
}),
// Console logger
new transports.Console({
format: logger.format.combine(
format.timestamp({
format: 'HH:mm:ss'
}),
format.colorize(),
format.printf(info => `${info.timestamp} [${info.level}]: ${info.message} ${Object.keys(info.metadata).length > 0 ? '\n' + JSON.stringify(info.metadata) : ''}`),
format.align(),
),
level: 'info'
})
]
});
logger.add(mainLoggers);
logger.log = logger.info; // Set alias, similar to console.log;
module.exports = logger;