-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
41 lines (35 loc) · 1.33 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
import path from 'path'
import { createLogger, format, transports, config as winstonConfig } from 'winston'
import config from './config'
const { combine, timestamp, printf, json } = format;
const myFormat = printf(({ level, message, timestamp, meta }) => {
const metaData = meta ? JSON.stringify(meta) : ''
return `[${level}] [${timestamp}]: ${message} ${metaData}`;
});
const logger = createLogger({
levels: winstonConfig.syslog.levels,
format: combine(
timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
json()
),
transports: [
new transports.File({ filename: path.join(__dirname, 'logs', 'alert.log'), level: 'alert' }),
new transports.File({ filename: path.join(__dirname, 'logs', 'error.log'), level: 'error' }),
new transports.File({ filename: path.join(__dirname, 'logs', 'combined.log'), level: 'notice' }),
new transports.File({ filename: path.join(__dirname, 'logs', 'combined.log'), level: 'warn' }),
new transports.File({ filename: path.join(__dirname, 'logs', 'combined.log'), level: 'error' })
],
exitOnError: false
})
if (config.NODEENV !== 'production') {
logger.add(new transports.Console({
format: combine(
format.colorize(),
timestamp(),
myFormat
)
}))
}
export default logger