-
Notifications
You must be signed in to change notification settings - Fork 5
/
log.js
executable file
·64 lines (59 loc) · 1.44 KB
/
log.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
import winston from 'winston';
import fs from 'fs';
import 'winston-daily-rotate-file';
// log directory based on environment
let logDir = 'devLog';
if (process.env.NODE_ENV === 'prod') {
logDir = 'prodLog';
} else if (process.env.NODE_ENV === 'staging') {
logDir = 'stagLog';
} else if (process.env.NODE_ENV === 'dev') {
logDir = 'devLog';
}
// Create the log directory if it does not exist
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const tsFormat = () => (new Date().toLocaleTimeString());
const logger = winston.createLogger({
transports: [
new winston.transports.Console({
level: 'debug',
timestamp: tsFormat,
// handleExceptions: true,
// json: true,
prettyPrint: true,
colorize: true,
}),
new winston.transports.DailyRotateFile({
level: 'info',
filename: `${logDir}/info-logs/-results.log`,
timestamp: new Date().toLocaleTimeString(),
datePattern: 'DD-MM-YYYY',
json: true,
prettyPrint: true,
prepend: true,
eol: '\n\n',
}),
new winston.transports.DailyRotateFile({
level: 'error',
name: 'error',
filename: `${logDir}/error-logs/-results.log`,
timestamp: tsFormat(),
datePattern: 'DD-MM-YYYY',
json: false,
handleExceptions: false,
eol: '\n\n\n',
}),
],
exitOnError: false,
});
const stream = {
write: (message) => {
logger.info(message);
},
};
export {
logger,
stream,
};