-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
52 lines (47 loc) · 1.01 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
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, printf, json, colorize } = format;
// Define custom log levels
const customLevels = {
levels: {
error: 0,
warn: 1,
info: 2,
http: 3,
verbose: 4,
debug: 5,
silly: 6
},
colors: {
error: 'red',
warn: 'yellow',
info: 'green',
http: 'magenta',
verbose: 'cyan',
debug: 'blue',
silly: 'gray'
}
};
// Define custom log format
const logFormat = printf(({ level, message, timestamp }) => {
return `${timestamp} [${level}]: ${message}`;
});
// Create the logger
const logger = createLogger({
levels: customLevels.levels,
format: combine(
timestamp(),
logFormat
),
transports: [
new transports.Console({
format: combine(
colorize(),
logFormat
)
}),
new transports.File({ filename: 'app.log', format: json() })
]
});
// Add colors to the custom levels
require('winston').addColors(customLevels.colors);
module.exports = logger;