Skip to content

Commit

Permalink
fix: use memory logger instance when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Nov 7, 2023
1 parent d20ec2a commit c2ef3a0
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

3 comments on commit c2ef3a0

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: c2ef3a0 Previous: 8cb21ce Ratio
Child logger creation 480471 ops/sec (±1.80%) 705998 ops/sec (±1.56%) 1.47
Logging a string on root logger 830939 ops/sec (±5.86%) 769369 ops/sec (±9.53%) 0.93
Logging an object on root logger 508485 ops/sec (±7.87%) 599796 ops/sec (±6.73%) 1.18
Logging an object with a message on root logger 15488 ops/sec (±187.13%) 9546 ops/sec (±206.83%) 0.62
Logging an object with a redacted prop on root logger 376461 ops/sec (±7.28%) 499136 ops/sec (±7.10%) 1.33
Logging a nested 3-level object on root logger 328781 ops/sec (±7.64%) 376470 ops/sec (±9.01%) 1.15

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: c2ef3a0 Previous: 8cb21ce Ratio
Child logger creation 348225 ops/sec (±0.35%) 379781 ops/sec (±5.78%) 1.09
Logging a string on root logger 825105 ops/sec (±6.01%) 425006 ops/sec (±12.02%) 0.52
Logging an object on root logger 652905 ops/sec (±7.64%) 326181 ops/sec (±19.18%) 0.50
Logging an object with a message on root logger 23494 ops/sec (±184.38%) 207252 ops/sec (±11.72%) 8.82
Logging an object with a redacted prop on root logger 476442 ops/sec (±6.37%) 257608 ops/sec (±18.55%) 0.54
Logging a nested 3-level object on root logger 328575 ops/sec (±5.44%) 6125 ops/sec (±189.83%) 0.01864110172715514

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Logger Benchmarks - windows-latest'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: c2ef3a0 Previous: 8cb21ce Ratio
Logging an object with a message on root logger 23494 ops/sec (±184.38%) 207252 ops/sec (±11.72%) 8.82

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.