From 4b8750edc2aeb6c9918b281569c223d51fbaae07 Mon Sep 17 00:00:00 2001 From: wood Date: Sun, 21 Jul 2024 20:11:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A6=81=E7=94=A8=E6=97=A5=E5=BF=97=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9=EF=BC=8C=E6=97=A5=E5=BF=97=E5=86=85=E5=AE=B9=E6=8E=92?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nodejs/app.js | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/nodejs/app.js b/nodejs/app.js index ed7bbeb..83977fd 100644 --- a/nodejs/app.js +++ b/nodejs/app.js @@ -23,38 +23,45 @@ const csvCache = new LRU({ maxAge: 1000 * 60 * 60 * 24 // 缓存24小时 }); -//日志格式 +//日志名称格式 const consoleFormat = winston.format.printf(({ level, message, timestamp }) => { return `${timestamp} ${level}: ${message}`; }); +// 自定义日志格式 +const customFormat = winston.format.printf(({ level, message, timestamp, ...metadata }) => { + let msg = `${timestamp} [${level}]: `; + + if (typeof message === 'object') { + msg += JSON.stringify(message, null, 2); + } else { + msg += message; + } + + if (metadata.logs) { + msg += '\n' + metadata.logs.map(log => JSON.stringify(log, null, 2)).join('\n'); + } + + return msg; +}); // 设置日志 const logger = winston.createLogger({ level: 'info', format: winston.format.combine( - winston.format.timestamp(), - winston.format.json() + winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), + customFormat ), transports: [ - new winston.transports.Console({ - format: winston.format.combine( - winston.format.timestamp(), - consoleFormat - ), - }), + new winston.transports.Console(), new winston.transports.DailyRotateFile({ filename: 'logs/application-%DATE%.log', datePattern: 'YYYY-MM-DD-HH', - zippedArchive: true, maxSize: '20m', - maxFiles: '14d' + maxFiles: '7d', //日志保存的时间,超过这个时间的会自动删除旧文件 + zippedArchive: false // 禁用压缩 }) ] }); - -// 使用压缩中间件 -app.use(compression()); - // 日志缓冲 let logBuffer = []; @@ -75,7 +82,7 @@ app.use((req, res, next) => { }; // 立即输出到控制台 - console.log(JSON.stringify(logEntry)); + logger.info(logEntry); // 添加到缓冲区,用于每小时写入文件 logBuffer.push(logEntry); @@ -83,7 +90,7 @@ app.use((req, res, next) => { next(); }); -// 每小时输出一次日志 +// 每小时输出一次日志到文件 setInterval(() => { if (logBuffer.length > 0) { logger.info('Hourly log', { logs: logBuffer });