-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
74 lines (64 loc) · 1.56 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
import path from "path"
import winston from "winston"
const initializeLogger = () => {
winston.exceptions.handle = () => {
//
}
// NYPL levels allowed by console
const nyplLogLevels = {
error: 1,
warn: 2,
info: 3,
debug: 4,
}
const getLogLevelCode = (levelString) => {
switch (levelString) {
case "error":
return 1
case "warn":
return 2
case "info":
return 3
case "debug":
return 4
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({ level: "debug" }),
new winston.transports.File({
filename: path.resolve(process.cwd(), "log", "rc.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