Skip to content

Commit

Permalink
Merge pull request #406 from seratch/issue-405
Browse files Browse the repository at this point in the history
Fix #405 by providing a better way to configure logger
  • Loading branch information
seratch authored Feb 19, 2020
2 parents a2f546d + 4f3f3ea commit 217fe72
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 @@ -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', () => {
Expand Down
30 changes: 22 additions & 8 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
});
}
}

Expand Down

0 comments on commit 217fe72

Please sign in to comment.