All checks were successful
CI Monitor MIflow / build-image (push) Successful in 2m42s
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import "winston-daily-rotate-file"
|
|
|
|
import winston, { format } from "winston"
|
|
|
|
const isProd = process.env.NODE_ENV === "production"
|
|
|
|
const dailyRotateFileTransport = new winston.transports.DailyRotateFile({
|
|
filename: "./log/application-%DATE%.log",
|
|
datePattern: "YYYY-MM-DD",
|
|
zippedArchive: true,
|
|
maxSize: "20m",
|
|
maxFiles: "14d",
|
|
})
|
|
|
|
const transports: any[] = [new winston.transports.Console()]
|
|
if (isProd) {
|
|
transports.push(dailyRotateFileTransport)
|
|
}
|
|
|
|
const loggerIns = winston.createLogger({
|
|
level: "info",
|
|
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
|