gitlab_monitor/log/index.ts
zhaoyingbo b530229e90
All checks were successful
CI Monitor MIflow / build-image (push) Successful in 59s
feat: 修改打印逻辑 & 请求等待结果
2024-08-13 01:30:15 +00:00

55 lines
1.4 KiB
TypeScript

import "winston-daily-rotate-file"
import winston, { format } from "winston"
const isProd = process.env.NODE_ENV === "production"
const dailyRotateFileTransport4Info = new winston.transports.DailyRotateFile({
level: "info",
filename: "/home/work/log/egg-info-%DATE%.log",
datePattern: "YYYY-MM-DD",
zippedArchive: true,
maxSize: "20m",
maxFiles: "14d",
})
const dailyRotateFileTransport4Debug = new winston.transports.DailyRotateFile({
level: "debug",
filename: "/home/work/log/egg-debug-%DATE%.log",
datePattern: "YYYY-MM-DD",
zippedArchive: true,
maxSize: "20m",
maxFiles: "14d",
})
const transports: any[] = [
new winston.transports.Console({
level: "info",
}),
]
if (isProd) {
transports.push(dailyRotateFileTransport4Info)
transports.push(dailyRotateFileTransport4Debug)
}
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