-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging.js
More file actions
90 lines (78 loc) · 2.23 KB
/
logging.js
File metadata and controls
90 lines (78 loc) · 2.23 KB
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
85
86
87
88
89
90
const winston = require("winston")
const WinstonDaily = require("winston-daily-rotate-file") //날짜별로 로그 저장
const path = require("path")
const { combine, timestamp, printf, colorize } = winston.format
const moment = require("moment-timezone")
moment.tz.setDefault("Asia/Seoul")
const timezone = () => {
return moment().format("YYYY-MM-DD HH:mm:ss.SSS")
}
const logDir = process.env.LOGDIR || "logs"
const levels = {
error: 0,
warn: 1,
info: 2,
http: 3,
debug: 4,
}
const colors = {
error: "red",
warn: "yellow",
info: "green",
http: "magenta",
debug: "blue",
}
winston.addColors(colors)
const level = () => {
const env = process.env.NODE_ENV || "development"
const isDevelopment = env === "development"
return isDevelopment ? "debug" : "http"
}
const logFormat = combine(
timestamp({ format: timezone() }),
printf((info) => {
if (info.stack) {
return `${info.timestamp} ${info.level}: ${info.message} \n Error Stack: ${info.stack}`
}
return `${timezone(info.timestamp)} ${info.level}: ${info.message}`
})
)
const consoleOpts = {
handleExceptions: true,
level: process.env.NODE_ENV === "production" ? "error" : "debug",
format: combine(colorize({ all: true }), timestamp({ format: timezone() })),
}
const transports = [
// 콘솔로그찍을 때만 색넣자.
new winston.transports.Console(consoleOpts),
// error 레벨 로그를 저장할 파일 설정
new WinstonDaily({
level: "error",
datePattern: "YYYY-MM-DD",
dirname: path.join(logDir, "/error"),
filename: "%DATE%.error.log",
maxFiles: 30,
zippedArchive: true,
}),
// 모든 레벨 로그를 저장할 파일 설정
new WinstonDaily({
level: "debug",
datePattern: "YYYY-MM-DD",
dirname: path.join(logDir, "/all"),
filename: "%DATE%.all.log",
maxFiles: 7,
zippedArchive: true,
}),
]
const Logger = winston.createLogger({
level: level(),
levels,
format: logFormat,
transports,
})
const stream = {
write: (message) => {
Logger.info(message.substring(0, message.lastIndexOf("\n")))
},
}
module.exports = { Logger, stream }