Skip to content

Commit

Permalink
Merge pull request #979 from forcedotcom/sh/no-logger-creation-with-e…
Browse files Browse the repository at this point in the history
…nv-var

fix: use memory logger instance when disabled
  • Loading branch information
WillieRuemmele authored Nov 8, 2023
2 parents 93c9693 + c2ef3a0 commit a62834c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {} }
Expand All @@ -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(
{
Expand Down
17 changes: 17 additions & 0 deletions test/unit/loggerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit a62834c

Please sign in to comment.