Skip to content

Commit

Permalink
Merge pull request #413 from seratch/pr-406-v2
Browse files Browse the repository at this point in the history
[v2] Fix #405 by providing a better way to configure logger
  • Loading branch information
seratch authored Feb 19, 2020
2 parents 3400479 + d1a03fa commit 1428ec3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
50 changes: 50 additions & 0 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
30 changes: 22 additions & 8 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
});
}
}

Expand Down

0 comments on commit 1428ec3

Please sign in to comment.