From d42b6fd80f7e60a877913de3f10daf46754427b9 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Mon, 7 Oct 2024 10:04:57 +0200 Subject: [PATCH] fix: re-use file transport instance when setup loggers Fixes #2937 --- api/lib/logger.ts | 56 +++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/api/lib/logger.ts b/api/lib/logger.ts index 0d053c0ecc..dcad7a741b 100644 --- a/api/lib/logger.ts +++ b/api/lib/logger.ts @@ -19,6 +19,8 @@ export const defaultLogFile = 'z-ui_%DATE%.log' export const disableColors = process.env.NO_LOG_COLORS === 'true' +let fileTransport: winston.transport = null + // ensure store and logs directories exist ensureDirSync(storeDir) ensureDirSync(logsDir) @@ -130,31 +132,33 @@ export function customTransports(config: LoggerConfig): winston.transport[] { transportsList.push(streamTransport) if (config.logToFile) { - let fileTransport: winston.transport - if (process.env.DISABLE_LOG_ROTATION === 'true') { - fileTransport = new transports.File({ - format: customFormat(config, true), - filename: config.filePath, - level: config.level, - }) - } else { - const options: DailyRotateFileTransportOptions = { - filename: config.filePath, - auditFile: joinPath(logsDir, 'zui-logs.audit.json'), - datePattern: 'YYYY-MM-DD', - createSymlink: true, - symlinkName: path - .basename(config.filePath) - .replace(`_%DATE%`, '_current'), - zippedArchive: true, - maxFiles: process.env.ZUI_LOG_MAXFILES || '7d', - maxSize: process.env.ZUI_LOG_MAXSIZE || '50m', - level: config.level, - format: customFormat(config, true), - } - fileTransport = new DailyRotateFile(options) + // setup file transport only once (see issue #2937) + if (!fileTransport) { + if (process.env.DISABLE_LOG_ROTATION === 'true') { + fileTransport = new transports.File({ + format: customFormat(config, true), + filename: config.filePath, + level: config.level, + }) + } else { + const options: DailyRotateFileTransportOptions = { + filename: config.filePath, + auditFile: joinPath(logsDir, 'zui-logs.audit.json'), + datePattern: 'YYYY-MM-DD', + createSymlink: true, + symlinkName: path + .basename(config.filePath) + .replace(`_%DATE%`, '_current'), + zippedArchive: true, + maxFiles: process.env.ZUI_LOG_MAXFILES || '7d', + maxSize: process.env.ZUI_LOG_MAXSIZE || '50m', + level: config.level, + format: customFormat(config, true), + } + fileTransport = new DailyRotateFile(options) - setupCleanJob(options) + setupCleanJob(options) + } } transportsList.push(fileTransport) @@ -199,6 +203,10 @@ export function module(module: string): ModuleLogger { export function setupAll(config: DeepPartial) { stopCleanJob() + fileTransport.close() + + fileTransport = null + logContainer.loggers.forEach((logger: ModuleLogger) => { logger.setup(config) })