-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
33 lines (28 loc) · 965 Bytes
/
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
const { createLogger, format, transports } = require('winston');
const { inspect } = require('util');
function formatWithInspect(val) {
return `${val instanceof Object ? '\n' : ''} ${inspect(val, { depth: null, colors: true })}`;
}
const logLevel = process.env.NIGHTLITE_LOG_LEVEL ? process.env.NIGHTLITE_LOG_LEVEL : 'info';
module.exports = createLogger({
level: logLevel,
format: format.combine(
format.errors({ stack: true }),
format.colorize(),
format.printf(info => {
const splatArgs = info[Symbol.for('splat')];
let log = `${info.level}: ${info.message}`;
// append splat messages to log
if (splatArgs) {
const rest = splatArgs.map(data => formatWithInspect(data)).join();
log += ` ${rest}`;
}
// check if error log, if so append error stack
if (info.stack) {
log += ` ${info.stack}`;
}
return log;
}),
),
transports: [new transports.Console()],
});