Skip to content

Commit

Permalink
fix: add debug logger that accepts a function
Browse files Browse the repository at this point in the history
  • Loading branch information
tnoonan-salesforce committed Aug 29, 2019
1 parent 8c77571 commit 117dee4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,22 @@ export class Logger {
return this;
}

/**
* Logs at `debug` level with filtering applied.
*
* @param cb A callback that returns on array objects to be logged.
*/
public debugCallback(cb: () => Array<unknown> | string) {
if (this.getLevel() === LoggerLevel.DEBUG || process.env.DEBUG) {
const result = cb();
if (isArray(result)) {
this.bunyan.debug(this.applyFilters(LoggerLevel.DEBUG, ...result));
} else {
this.bunyan.debug(this.applyFilters(LoggerLevel.DEBUG, ...[result]));
}
}
}

/**
* Logs at `info` level with filtering applied. For convenience `this` object is returned.
*
Expand Down
23 changes: 23 additions & 0 deletions test/unit/loggerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,29 @@ describe('Logger', () => {
});
});

describe('debugCallback', () => {
it('should log', async () => {
const logger = (await Logger.child('testLogger')).useMemoryLogging();
logger.setLevel(LoggerLevel.DEBUG);
const FOO = 'foo';
const BAR = 'bar';
const spy = $$.SANDBOX.spy(() => [FOO, BAR]);
logger.debugCallback(spy);
expect(spy.callCount).to.be.equal(1);
expect(logger.readLogContentsAsText())
.to.include(FOO)
.and.to.include(BAR);
});

it("shouldn't log", async () => {
const logger = (await Logger.child('testLogger')).useMemoryLogging();
const fooSpy = $$.SANDBOX.spy(() => 'FOO');
const cbSpy = $$.SANDBOX.spy(() => `${fooSpy()}`);
logger.debugCallback(cbSpy);
expect(fooSpy.callCount).to.be.equal(0);
expect(cbSpy.callCount).to.be.equal(0);
});
});
describe('filters', () => {
const sid = 'SIDHERE!';
const simpleString = `sid=${sid}`;
Expand Down

0 comments on commit 117dee4

Please sign in to comment.