-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.js
36 lines (31 loc) · 1.03 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
const winston = require('winston')
const customFormat = winston.format.printf(({ level, message, timestamp, ...metadata }) => {
let msg = `${level.toUpperCase()} [${formatDate(new Date(timestamp))}] ${message} `
for (const key in metadata) {
msg += `${key}:= ${metadata[key]} `
}
return msg
})
function formatDate (date) {
const pad = (num) => num.toString().padStart(2, '0')
const padMilliseconds = (num) => num.toString().padStart(3, '0')
const day = pad(date.getDate())
const month = pad(date.getMonth() + 1)
const hours = pad(date.getHours())
const minutes = pad(date.getMinutes())
const seconds = pad(date.getSeconds())
const milliseconds = padMilliseconds(date.getMilliseconds())
return `${month}-${day}|${hours}:${minutes}:${seconds}.${milliseconds}`
}
const logger = (level) => winston.createLogger({
level,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.ms(),
customFormat
),
transports: [
new winston.transports.Console()
]
})
module.exports = logger