-
Notifications
You must be signed in to change notification settings - Fork 7
/
logger.js
83 lines (75 loc) · 2.16 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @module logger
* Just logger
*
* // throw new Error('error')
// logger.debug('Debugging info')
// logger.verbose('Verbose info')
// logger.info('Hello world')
// logger.warn('Warning message')
// logger.error('Error info')
*/
'use strict'
const { createLogger, format, transports } = require('winston')
require('winston-daily-rotate-file')
const config = require('./config')
const fs = require('fs')
const path = require('path')
const env = config.env || 'development'
const logDir = path.join(__dirname, 'log')
// Create the log directory if it does not exist
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir)
}
const dailyRotateFileTransportResults = new transports.DailyRotateFile({
filename: `${logDir}/%DATE%-results.log`,
datePattern: 'YYYY-MM-DD',
maxSize: '20m',
maxFiles: '14d'
})
const dailyRotateFileTransportErrors = new transports.DailyRotateFile({
filename: `${logDir}/%DATE%-errors.log`,
datePattern: 'YYYY-MM-DD',
maxSize: '20m',
maxFiles: '14d'
})
const logger = createLogger({
// change level if in dev environment versus production
// To the file
// Note: only level or greater will log to the console
level: 'debug',
format: format.combine(
format.timestamp({
format: 'HH:mm:ss'
}),
format.printf(
info => `${info.timestamp} ${info.level}: ${info.message}`
)
),
// On the screen
transports: [
new transports.Console({
level: env === 'development' ? 'debug' : 'info',
format: format.combine(
format.colorize(),
format.printf(
info => `${info.timestamp} ${info.level}: ${info.message}`
)
)
}),
dailyRotateFileTransportResults
],
exceptionHandlers: [
new transports.Console({
level: 'error',
format: format.combine(
format.colorize(),
format.printf(
info => `${info.timestamp} ${info.level}: ${info.message}`
)
)
}),
dailyRotateFileTransportErrors
]
})
module.exports = logger