-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.js
60 lines (45 loc) · 1.32 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
/// Custom Winston logger setup
const { createLogger, format, transports, addColors } = require("winston");
/// { error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 }
const level = process.env.LOG_LEVEL || "debug";
function formatParams(info) {
const { timestamp, level, message, ...args } = info;
const ts = timestamp.slice(0, 19).replace("T", " ");
return `${ts} ${level}: ${message} ${Object.keys(args).length
? JSON.stringify(args, "", "")
: ""}`;
}
function formatParamsDev(info) {
const { level, message, ...args } = info;
return `${level}: ${message} ${Object.keys(args).length
? JSON.stringify(args, "", "")
: ""}`;
}
const developmentFormat = format.combine(
format.colorize(),
format.align(),
format.printf(formatParamsDev)
);
const productionFormat = format.combine(
format.timestamp(),
format.align(),
format.printf(formatParams)
);
let logger;
if (process.env.NODE_ENV !== "production") {
logger = createLogger({
level: level,
format: developmentFormat,
transports: [new transports.Console()]
});
} else {
logger = createLogger({
level: level,
format: productionFormat,
transports: [
new transports.File({ filename: "error.log", level: "error" }),
new transports.File({ filename: "combined.log" })
]
});
}
module.exports = logger