-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
60 lines (54 loc) · 1.99 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const util = require('util')
const { extend } = require('moleculer').Logger
const winston = require('winston')
const moment = require('moment')
const chalk = require('chalk')
const _ = require('lodash')
const { getConfig } = require('@bit/amazingdesign.utils.config')
const LOGGER_HTTP_HOST = getConfig('LOGGER_HTTP_HOST')
const LOGGER_HTTP_PORT = getConfig('LOGGER_HTTP_PORT')
const LOGGER_HTTP_PATH = getConfig('LOGGER_HTTP_PATH')
const logger = bindings => extend(winston.createLogger({
format: winston.format.combine(
winston.format.label({ label: bindings }),
winston.format.timestamp(),
winston.format.json(),
),
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(({ level, message, label, timestamp }) => {
const getColor = type => {
switch (type) {
case 'fatal': return chalk.red.inverse
case 'error': return chalk.red
case 'warn': return chalk.yellow
case 'debug': return chalk.magenta
case 'trace': return chalk.gray
default: return chalk.green
}
}
const getType = type => getColor(type)(_.padEnd(type.toUpperCase(), 5))
const objectPrinter = o => util.inspect(
o,
{ showHidden: false, depth: 2, colors: chalk.enabled, breakLength: Number.POSITIVE_INFINITY }
)
const time = chalk.grey(`[${moment(timestamp).format('YYYY-MM-DD HH:mm:ss.SSS')}]`)
const displayedMessage = typeof message === 'object' ? objectPrinter(message) : message
return `${time} ${getType(level)} ${chalk.grey(label.mod)} :\n${displayedMessage}`
}),
)
}),
].concat(
(
LOGGER_HTTP_HOST &&
new winston.transports.Http({
host: LOGGER_HTTP_HOST,
port: LOGGER_HTTP_PORT || 80,
path: LOGGER_HTTP_PATH || '/',
})
) || []
)
}))
module.exports = logger