Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #405 by providing a better way to configure logger #406

Merged
merged 2 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we still setting LogLevel.INFO as the default somewhere? Looks like we lost that logic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay rad!

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');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone has suggestions for the default name, leave comments here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me

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