Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: re-use file transport instance when setup loggers #3928

Merged
merged 7 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion api/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const defaultLogFile = 'z-ui_%DATE%.log'

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

let transportsList: winston.transport[] = null

// ensure store and logs directories exist
ensureDirSync(storeDir)
ensureDirSync(logsDir)
Expand Down Expand Up @@ -109,7 +111,12 @@ export const logStream = new PassThrough()
* Create the base transports based on settings provided
*/
export function customTransports(config: LoggerConfig): winston.transport[] {
const transportsList: winston.transport[] = []
// setup transports only once (see issue #2937)
if (transportsList) {
return transportsList
}

transportsList = []

if (process.env.ZUI_NO_CONSOLE !== 'true') {
transportsList.push(
Expand All @@ -131,6 +138,7 @@ export function customTransports(config: LoggerConfig): winston.transport[] {

if (config.logToFile) {
let fileTransport: winston.transport

if (process.env.DISABLE_LOG_ROTATION === 'true') {
fileTransport = new transports.File({
format: customFormat(config, true),
Expand Down Expand Up @@ -159,6 +167,13 @@ export function customTransports(config: LoggerConfig): winston.transport[] {

transportsList.push(fileTransport)
}

// giving that we re-use transports, each module will subscribe to events
// increeasing the default limit of 100 prevents warnings
transportsList.forEach((t) => {
t.setMaxListeners(100)
})

return transportsList
}

Expand Down Expand Up @@ -199,6 +214,14 @@ export function module(module: string): ModuleLogger {
export function setupAll(config: DeepPartial<GatewayConfig>) {
stopCleanJob()

transportsList.forEach((t) => {
if (typeof t.close === 'function') {
t.close()
}
})

transportsList = null

logContainer.loggers.forEach((logger: ModuleLogger) => {
logger.setup(config)
})
Expand Down
4 changes: 2 additions & 2 deletions test/lib/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('logger.js', () => {
it('should change the log level', () =>
expect(logger1.level).to.equal('warn'))
it('should have 2 transports', () =>
expect(logger1.transports.length).to.be.equal(3))
expect(logger1.transports.length).to.be.equal(2))
})

describe('setup() (reconfigure)', () => {
Expand All @@ -118,7 +118,7 @@ describe('logger.js', () => {
// Test post-conditions:
expect(logger1.module).to.equal('mod')
expect(logger1.level).to.equal('error')
expect(logger1.transports.length).to.be.equal(3)
expect(logger1.transports.length).to.be.equal(2)
})
})

Expand Down
Loading