-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (72 loc) · 1.97 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* @flow */
const winston = require('winston')
const path = require('path')
/**
* creates new custom winston logger
*
* @param {object} config - koop configuration
* @return {Logger} custom logger instance
*/
function createLogger (config) {
config = config || {}
let level
if (process.env.KOOP_LOG_LEVEL) {
level = process.env.KOOP_LOG_LEVEL
} else if (process.env.NODE_ENV === 'production') {
level = 'info'
} else {
level = 'debug'
}
if (!config.logfile) {
// no logfile defined, log to STDOUT an STDERRv
const debugConsole = new winston.transports.Console({
colorize: process.env.NODE_ENV === 'production',
level,
stringify: true,
json: true
})
return winston.createLogger({ transports: [debugConsole] })
}
// we need a dir to do log rotation so we get the dir from the file
const logpath = path.dirname(config.logfile)
const logAll = new winston.transports.File({
filename: config.logfile,
name: 'log.all',
dirname: logpath,
colorize: true,
json: false,
level,
formatter: formatter
})
const logError = new winston.transports.File({
filename: config.logfile.replace('.log', '.error.log'),
name: 'log.error',
dirname: logpath,
colorize: true,
json: false,
level: 'error',
formatter: formatter
})
// always log errors
const transports = [logError]
// only log everthing if debug mode is on
if (process.env.LOG_LEVEL === 'debug') {
transports.push(logAll)
}
return winston.createLogger({ transports })
}
/**
* formats winston log lines
* @param {object} options - log info from winston
* @return {string} formatted log line
*/
function formatter (options) {
const line = [
new Date().toISOString(),
options.level
]
if (options.message !== undefined) line.push(options.message)
if (options.meta && Object.keys(options.meta).length) line.push(JSON.stringify(options.meta))
return line.join(' ')
}
module.exports = createLogger