43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
import "winston-daily-rotate-file";
|
|
|
|
import winston, { format } from "winston";
|
|
const isProd = process.env.NODE_ENV === "production";
|
|
const transports = [
|
|
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;
|