From d45f209bec03e95e8d5b5371dd020c4b22bfa585 Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Sat, 15 Feb 2020 12:40:35 +0900 Subject: [PATCH] Fix #405 by providing a better way to configure logger --- src/App.spec.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ src/App.ts | 30 +++++++++++++++++++++-------- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/src/App.spec.ts b/src/App.spec.ts index 6ae73d5e0..1f46523cd 100644 --- a/src/App.spec.ts +++ b/src/App.spec.ts @@ -604,6 +604,56 @@ describe('App', () => { assert.isTrue(fakeLogger.info.called); assert.isTrue(fakeLogger.debug.called); }); + + it('should work in the case both logger and logLevel are given', async () => { + // Arrange + const App = await importApp(overrides); // tslint:disable-line:variable-name + const fakeLogger = createFakeLogger(); + const app = new App({ + logger: fakeLogger, + logLevel: LogLevel.DEBUG, + receiver: fakeReceiver, + authorize: sinon.fake.resolves(dummyAuthorizationResult), + }); + app.use(({ logger, body, next }) => { + logger.info(body); + next(); + }); + + app.event('app_home_opened', ({ logger, event }) => { + logger.debug(event); + }); + + const receiverEvents = [ + { + body: { + type: 'event_callback', + token: 'XXYYZZ', + team_id: 'TXXXXXXXX', + api_app_id: 'AXXXXXXXXX', + event: { + type: 'app_home_opened', + event_ts: '1234567890.123456', + user: 'UXXXXXXX1', + text: 'hello friends!', + tab: 'home', + view: {}, + }, + }, + respond: noop, + ack: noop, + }, + ]; + + // Act + receiverEvents.forEach(event => fakeReceiver.emit('message', event)); + await delay(); + + // Assert + assert.isTrue(fakeLogger.info.called); + assert.isTrue(fakeLogger.debug.called); + assert.isTrue(fakeLogger.setLevel.called); + }); }); describe('client', () => { diff --git a/src/App.ts b/src/App.ts index 620826459..27b9a123d 100644 --- a/src/App.ts +++ b/src/App.ts @@ -155,20 +155,28 @@ export default class App { botId = undefined, botUserId = undefined, authorize = undefined, - logger = new ConsoleLogger(), - logLevel = LogLevel.INFO, + logger = undefined, + logLevel = undefined, ignoreSelf = true, clientOptions = undefined, }: AppOptions = {}) { - this.logger = logger; - this.logger.setLevel(logLevel); + if (typeof logger === 'undefined') { + // Initialize with the default logger + const consoleLogger = new ConsoleLogger(); + consoleLogger.setName('bolt-app'); + this.logger = consoleLogger; + } else { + this.logger = logger; + } + if (typeof logLevel !== 'undefined' && this.logger.getLevel() !== logLevel) { + this.logger.setLevel(logLevel); + } this.errorHandler = defaultErrorHandler(this.logger); - this.clientOptions = { agent, - logLevel, - logger, + // App propagates only the log level to WebClient as WebClient has its own logger + logLevel: this.logger.getLevel(), tls: clientTls, slackApiUrl: clientOptions !== undefined ? clientOptions.slackApiUrl : undefined, }; @@ -208,7 +216,13 @@ export default class App { ); } else { // Create default ExpressReceiver - this.receiver = new ExpressReceiver({ signingSecret, logger, endpoints, agent, clientTls }); + this.receiver = new ExpressReceiver({ + signingSecret, + endpoints, + agent, + clientTls, + logger: this.logger, + }); } }