Skip to content

Commit

Permalink
fix: log uncaught exception in root logger only
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoyi Chen committed Jun 10, 2020
1 parent d14591a commit 7a80662
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,15 @@ export class Logger {
}

private uncaughtExceptionHandler = (err: Error) => {
// log the exception
// FIXME: good chance this won't be logged because
// process.exit was called before this is logged
// https://github.com/trentm/node-bunyan/issues/95
this.fatal(err);
// W-7558552
// Only log uncaught exceptions in root logger
if (this === Logger.rootLogger) {
// log the exception
// FIXME: good chance this won't be logged because
// process.exit was called before this is logged
// https://github.com/trentm/node-bunyan/issues/95
this.fatal(err);
}
};

private exitHandler = () => {
Expand Down
23 changes: 23 additions & 0 deletions test/unit/loggerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ describe('Logger', () => {
expect(Logger.root['called']).to.be.true;
expect(rootLogger.addLogFileStream['called']).to.be.false;
});

it('should log uncaught exception in root logger', async () => {
process.env.SFDX_ENV = 'dev';

$$.SANDBOX.stub(Logger.prototype, 'fatal');
const rootLogger = await Logger.root();

// @ts-ignore
Logger.lifecycle.emit('uncaughtException', 'testException');
expect(rootLogger.fatal['called']).to.be.true;
});
});

describe('child', () => {
Expand All @@ -166,6 +177,18 @@ describe('Logger', () => {
expect(childLogger).to.be.instanceof(Logger);
expect(childLogger.getName()).to.equal(childLoggerName);
});

it('should not log uncaught exception in child logger', async () => {
process.env.SFDX_ENV = 'dev';

$$.SANDBOX.stub(Logger.prototype, 'fatal');
const childLoggerName = 'myChildLogger';
const childLogger = await Logger.child(childLoggerName);

// @ts-ignore
Logger.lifecycle.emit('uncaughtException', 'testException');
expect(childLogger.fatal['called']).to.be.true;
});
});

describe('debugCallback', () => {
Expand Down

0 comments on commit 7a80662

Please sign in to comment.