/
logger.js
45 lines (41 loc) · 1.2 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
const winston = require('winston'),
path = require('path')
const env = process.env.NODE_ENV,
prod = env === 'production'
const jsonFormatter = winston.format.combine(
winston.format.timestamp(),
winston.format.json()
)
const consoleFormatter = winston.format.combine(
winston.format.colorize(),
winston.format.timestamp(),
winston.format.align(),
winston.format.printf(info => `[${info.timestamp}] ${info.level}: ${info.message}`)
)
const logger = winston.createLogger({
level: prod ? 'info' : 'verbose',
format: winston.format.simple(),
transports: [
new winston.transports.File({
filename: path.join(__dirname, 'logs', 'error.log'),
level: 'error'
}),
new winston.transports.File({
filename: path.join(__dirname, 'logs', 'combined.log')
}),
new winston.transports.File({
filename: path.join(__dirname, 'logs', 'error.log.json'),
level: 'error',
format: jsonFormatter
}),
new winston.transports.File({
filename: path.join(__dirname, 'logs', 'combined.log.json'),
format: jsonFormatter
}),
new winston.transports.Console({
colorize: true,
format: consoleFormatter
})
]
})
module.exports = logger