Skip to content

Commit d42b6fd

Browse files
committed
fix: re-use file transport instance when setup loggers
Fixes #2937
1 parent e8667de commit d42b6fd

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

api/lib/logger.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export const defaultLogFile = 'z-ui_%DATE%.log'
1919

2020
export const disableColors = process.env.NO_LOG_COLORS === 'true'
2121

22+
let fileTransport: winston.transport = null
23+
2224
// ensure store and logs directories exist
2325
ensureDirSync(storeDir)
2426
ensureDirSync(logsDir)
@@ -130,31 +132,33 @@ export function customTransports(config: LoggerConfig): winston.transport[] {
130132
transportsList.push(streamTransport)
131133

132134
if (config.logToFile) {
133-
let fileTransport: winston.transport
134-
if (process.env.DISABLE_LOG_ROTATION === 'true') {
135-
fileTransport = new transports.File({
136-
format: customFormat(config, true),
137-
filename: config.filePath,
138-
level: config.level,
139-
})
140-
} else {
141-
const options: DailyRotateFileTransportOptions = {
142-
filename: config.filePath,
143-
auditFile: joinPath(logsDir, 'zui-logs.audit.json'),
144-
datePattern: 'YYYY-MM-DD',
145-
createSymlink: true,
146-
symlinkName: path
147-
.basename(config.filePath)
148-
.replace(`_%DATE%`, '_current'),
149-
zippedArchive: true,
150-
maxFiles: process.env.ZUI_LOG_MAXFILES || '7d',
151-
maxSize: process.env.ZUI_LOG_MAXSIZE || '50m',
152-
level: config.level,
153-
format: customFormat(config, true),
154-
}
155-
fileTransport = new DailyRotateFile(options)
135+
// setup file transport only once (see issue #2937)
136+
if (!fileTransport) {
137+
if (process.env.DISABLE_LOG_ROTATION === 'true') {
138+
fileTransport = new transports.File({
139+
format: customFormat(config, true),
140+
filename: config.filePath,
141+
level: config.level,
142+
})
143+
} else {
144+
const options: DailyRotateFileTransportOptions = {
145+
filename: config.filePath,
146+
auditFile: joinPath(logsDir, 'zui-logs.audit.json'),
147+
datePattern: 'YYYY-MM-DD',
148+
createSymlink: true,
149+
symlinkName: path
150+
.basename(config.filePath)
151+
.replace(`_%DATE%`, '_current'),
152+
zippedArchive: true,
153+
maxFiles: process.env.ZUI_LOG_MAXFILES || '7d',
154+
maxSize: process.env.ZUI_LOG_MAXSIZE || '50m',
155+
level: config.level,
156+
format: customFormat(config, true),
157+
}
158+
fileTransport = new DailyRotateFile(options)
156159

157-
setupCleanJob(options)
160+
setupCleanJob(options)
161+
}
158162
}
159163

160164
transportsList.push(fileTransport)
@@ -199,6 +203,10 @@ export function module(module: string): ModuleLogger {
199203
export function setupAll(config: DeepPartial<GatewayConfig>) {
200204
stopCleanJob()
201205

206+
fileTransport.close()
207+
208+
fileTransport = null
209+
202210
logContainer.loggers.forEach((logger: ModuleLogger) => {
203211
logger.setup(config)
204212
})

0 commit comments

Comments
 (0)