From c2ef3a0f8fea0518b5eec2f6f515064e9741b46e Mon Sep 17 00:00:00 2001 From: Steve Hetzel Date: Tue, 7 Nov 2023 14:41:38 -0700 Subject: [PATCH] fix: use memory logger instance when disabled --- src/logger/logger.ts | 6 ++++-- test/unit/loggerTest.ts | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/logger/logger.ts b/src/logger/logger.ts index f3ddad6b82..2ac684ee51 100644 --- a/src/logger/logger.ts +++ b/src/logger/logger.ts @@ -147,6 +147,8 @@ export class Logger { * `Logger`. */ public constructor(optionsOrName: LoggerOptions | string) { + const enabled = process.env.SFDX_DISABLE_LOG_FILE !== 'true' && process.env.SF_DISABLE_LOG_FILE !== 'true'; + const options: LoggerOptions = typeof optionsOrName === 'string' ? { name: optionsOrName, level: Logger.DEFAULT_LEVEL, fields: {} } @@ -167,9 +169,9 @@ export class Logger { name: options.name ?? Logger.ROOT_NAME, base: options.fields ?? {}, level, - enabled: process.env.SFDX_DISABLE_LOG_FILE !== 'true' && process.env.SF_DISABLE_LOG_FILE !== 'true', + enabled, }; - if (options.useMemoryLogger || Global.getEnvironmentMode() === Mode.TEST) { + if (options.useMemoryLogger || Global.getEnvironmentMode() === Mode.TEST || !enabled) { this.memoryLogger = new MemoryLogger(); this.pinoLogger = pino( { diff --git a/test/unit/loggerTest.ts b/test/unit/loggerTest.ts index c4e701a4af..68dbaeef76 100644 --- a/test/unit/loggerTest.ts +++ b/test/unit/loggerTest.ts @@ -45,6 +45,23 @@ describe('Logger', () => { const logger2 = await Logger.root(); expect(logger2).to.not.equal(logger1); }); + + describe('DISABLE_LOG_FILE', () => { + const LOG_FILES_DISABLED = process.env.SF_DISABLE_LOG_FILE; + before(() => { + process.env.SF_DISABLE_LOG_FILE = 'true'; + }); + after(() => { + process.env.SF_DISABLE_LOG_FILE = LOG_FILES_DISABLED; + }); + it('should construct a new named logger', async () => { + const logger1 = new Logger({ name: 'testLogger-noop' }); + expect(logger1).to.be.instanceof(Logger); + // @ts-expect-error testing a private property + expect(logger1.memoryLogger).to.be.ok; + expect(logger1.getName()).to.equal('testLogger-noop'); + }); + }); }); describe('levels', () => {