-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.js
More file actions
35 lines (30 loc) · 891 Bytes
/
Copy pathlogging.js
File metadata and controls
35 lines (30 loc) · 891 Bytes
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
import winston from 'winston'
const { createLogger, format, transports } = winston
function trimString(n, s) {
return s.padStart(n).slice(-n)
}
const trimLevel = format((info, opts) => {
return { ...info, level: trimString(6, info.level) }
})
const loggers = {}
export default function getLogger(name, level) {
if (loggers[name]) {
return loggers[name]
}
return (loggers[name] = createLogger({
level: level || process.env.LOG_LEVEL || 'silly',
format: format.combine(
format.errors({ stack: true }),
format.label({ label: trimString(8, name) }),
format.timestamp(),
trimLevel(),
format.splat(),
format.colorize(),
format.printf(
({ timestamp, level, label, message, stack }) =>
`${timestamp} ${level} [${label}] ${message} ${stack || ''}`
)
),
transports: new transports.Console(),
}))
}