-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
84 lines (74 loc) · 1.78 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
84
import path from "path";
import winston from "winston";
const initializeLogger = () => {
winston.exceptions.handle = () => {};
// Set default NYPL agreed upon log levels
const nyplLogLevels = {
emergency: 0,
alert: 1,
critical: 2,
error: 3,
warning: 4,
notice: 5,
info: 6,
debug: 7,
};
const getLogLevelCode = (levelString) => {
switch (levelString) {
case "emergency":
return 0;
case "alert":
return 1;
case "critical":
return 2;
case "error":
return 3;
case "warning":
return 4;
case "notice":
return 5;
case "info":
return 6;
case "debug":
return 7;
default:
return "n/a";
}
};
const { combine, timestamp, printf } = winston.format;
const formatter = printf((info) => {
const timestamp = new Date().toISOString();
const logObject = {
timestamp,
pid: process.pid,
level: info.level,
levelCode: getLogLevelCode(info.level),
message: info.message,
};
return JSON.stringify(logObject);
});
const transports = [
new winston.transports.Console(),
new winston.transports.File({
filename: path.resolve(process.cwd(), "log", "dc.log"),
// Log format space limited
format: combine(winston.format.uncolorize(), formatter),
maxsize: 5242880,
maxFiles: 5,
}),
];
return winston.createLogger({
levels: nyplLogLevels,
level: process.env.LOG_LEVEL || "info",
format: combine(
timestamp({
format: "YYYY-MM-DD hh:mm:ss.SSS A",
}),
formatter
),
transports,
});
};
const isRunningOnVercel = process.env.VERCEL === "1";
const logger = isRunningOnVercel ? console : initializeLogger();
export default logger;