gitlab_monitor/log/index.ts
zhaoyingbo 878532997b
All checks were successful
CI Monitor MIflow / build-image (push) Successful in 55s
fix: 日志打印错误
2024-08-13 02:07:16 +00:00

56 lines
1.3 KiB
TypeScript

import "winston-daily-rotate-file"
import winston, { format } from "winston"
const isProd = process.env.NODE_ENV === "production"
const transports: any[] = [
new winston.transports.Console({
level: "info",
}),
]
if (isProd) {
const config = {
datePattern: "YYYY-MM-DD",
zippedArchive: true,
maxSize: "20m",
maxFiles: "14d",
}
transports.push(
new winston.transports.DailyRotateFile({
level: "info",
filename: "/home/work/log/egg-info-%DATE%.log",
...config,
})
)
transports.push(
new winston.transports.DailyRotateFile({
level: "debug",
filename: "/home/work/log/egg-debug-%DATE%.log",
...config,
})
)
}
const loggerIns = winston.createLogger({
level: "silly",
format: format.combine(
format.colorize({
level: !isProd,
}), // 开发环境下输出彩色日志
format.simple(), // 简单文本格式化
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
format.printf(({ level, message, timestamp, requestId }) => {
const singleLineMessage = isProd
? message.replace(/\n/g, " ") // 将换行符替换为空格
: message
return `${timestamp} [${level}] ${requestId ? `[RequestId: ${requestId}]` : ""}: ${singleLineMessage}`
})
),
transports,
})
export default loggerIns