-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
45 lines (40 loc) · 1.24 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
const {format, createLogger, transports} = require("winston");
const {loadConfig} = require("./utils");
const {combine, timestamp, printf} = format;
const {File, Console} = transports;
loadConfig();
const isLogEnabled = process.env.LOG_ENABLED === 'true';
const myFormat = printf(({level, message, timestamp}) => {
return `${timestamp} | ${level} | ${message}`;
});
const rowFormat = combine(timestamp(), myFormat);
const logger = createLogger({
level: 'info',
format: rowFormat,
});
const loggerKeys = createLogger({
level: 'info',
format: rowFormat,
});
if (isLogEnabled) {
logger.add(new File({filename: 'logs/error.log', level: 'error'}));
logger.add(new File({filename: 'logs/combined.log'}));
loggerKeys.add(new File({filename: 'logs/keys.log'}));
}
if (process.env.NODE_ENV !== 'production') {
// if we're not in production then log to the `console`
logger.add(new Console({
format: rowFormat
}));
loggerKeys.add(new Console({
format: rowFormat
}));
} else {
// in production enable handling uncaught exceptions if log is enabled
if (isLogEnabled) {
logger.exceptions.handle(
new File({filename: 'logs/exceptions.log'})
);
}
}
module.exports = {logger, loggerKeys};