-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (36 loc) · 1.19 KB
/
index.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
'use strict'
const winston = require('winston')
const loggers = new Set()
// log levels: error, warn, info, verbose, debug, silly
module.exports = (name, customFormatter) => {
if (!loggers.has(name)) {
winston.loggers.add(name, newLogger(name, customFormatter))
loggers.add(name)
}
return winston.loggers.get(name)
}
function newLogger (name, customFormatter) {
customFormatter = customFormatter || defaultFormatter
return {
console: {
name: name,
formatter: (options) => customFormatter(name, options)
}
}
}
// default formatter: plugin-name LEVEL timestamp message
function defaultFormatter (name, options) {
// Return string will be passed to logger.
let message = (options.message ? options.message : '')
const now = new Date().toISOString()
// if meta is an error, print error stack, else pretty print meta as JSON
if (options.meta) {
if (options.meta.stack) {
const stack = options.meta.stack
message += `\n${stack.join ? stack.join('\n') : stack}`
} else if (Object.keys(options.meta).length) {
message += `\n${JSON.stringify(options.meta, null, 2)}`
}
}
return `${options.level.toUpperCase()} ${now} ${name} ${message}`
}