diff --git a/src/App.spec.ts b/src/App.spec.ts index 89de7fb1b..7ae683618 100644 --- a/src/App.spec.ts +++ b/src/App.spec.ts @@ -694,6 +694,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(async ({ logger, body, next }) => { + logger.info(body); + await next(); + }); + + app.event('app_home_opened', async ({ 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 73a29048e..2fa54f2d1 100644 --- a/src/App.ts +++ b/src/App.ts @@ -165,20 +165,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, }; @@ -226,7 +234,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, + }); } }