-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathwinston.js
71 lines (63 loc) · 1.68 KB
/
winston.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
var appRoot = require('app-root-path')
var winston = require('winston')
const { combine, colorize, printf, timestamp } = winston.format
const logFormat = printf((info) => {
return `[${info.timestamp}] ${info.level}: ${info.message}`
})
const rawFormat = printf((info) => {
return `[${info.timestamp}] ${info.level}: ${info.message}`
})
// define the custom settings for each transport (file, console)
var options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false
},
errorFile: {
level: 'error',
name: 'file.error',
filename: `${appRoot}/logs/error.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 100,
colorize: true
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
format: combine(colorize(), rawFormat)
}
}
// instantiate a new Winston Logger with the settings defined above
var logger = winston.createLogger({
format: combine(
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
logFormat
),
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console)
],
exitOnError: false // do not exit on handled exceptions
})
// create a stream object with a 'write' function that will be used by `morgan`
logger.stream = {
write: function (message, encoding) {
// use the 'info' log level so the output will be picked up by both transports (file and console)
logger.info(message)
}
}
winston.addColors({
debug: 'white',
error: 'red',
info: 'green',
warn: 'yellow'
})
module.exports = logger