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: use memory logger instance when disabled #979

Merged
merged 1 commit into from
Nov 8, 2023
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
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
Loading