Skip to content

Commit

Permalink
feat: make the app logger attached to the app instance public (#2365)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimeg authored Dec 17, 2024
1 parent c091510 commit b6caa0a
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 38 deletions.
4 changes: 2 additions & 2 deletions docs/content/concepts/assistant.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ The following example uses the [OpenAI API client](https://platform.openai.com/d

```js
...
userMessage: async ({ client, message, say, setTitle, setStatus }) => {
userMessage: async ({ client, logger, message, say, setTitle, setStatus }) => {
const { channel, thread_ts } = message;

try {
Expand Down Expand Up @@ -155,7 +155,7 @@ The following example uses the [OpenAI API client](https://platform.openai.com/d
await say(llmResponse.choices[0].message.content);

} catch (e) {
console.error(e);
logger.error(e);

// Send message to advise user and clear processing status if a failure occurs
await say('Sorry, something went wrong!');
Expand Down
6 changes: 3 additions & 3 deletions docs/content/concepts/custom-routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const app = new App({

(async () => {
await app.start();
console.log('⚡️ Bolt app started');
app.logger.info('⚡️ Bolt app started');
})();
```

Expand Down Expand Up @@ -73,7 +73,7 @@ app.event('message', async ({ event, client }) => {

// Middleware methods execute on every web request
receiver.router.use((req, res, next) => {
console.log(`Request time: ${Date.now()}`);
app.logger.info(`Request time: ${Date.now()}`);
next();
});

Expand All @@ -85,6 +85,6 @@ receiver.router.post('/secret-page', (req, res) => {

(async () => {
await app.start();
console.log('⚡️ Bolt app started');
app.logger.info('⚡️ Bolt app started');
})();
```
2 changes: 1 addition & 1 deletion docs/content/concepts/deferring-initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const app = new App({
// Now safe to call start()
await app.start(process.env.PORT || 3000);
} catch (e) {
console.log(e);
app.logger.error(e);
process.exit(1);
}
})()
Expand Down
2 changes: 1 addition & 1 deletion docs/content/concepts/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const app = new App({
// A more generic, global error handler
app.error(async (error) => {
// Check the details of the error to handle cases where you should retry sending a message or stop the app
console.error(error);
app.logger.error(error);
});
```

Expand Down
31 changes: 30 additions & 1 deletion docs/content/concepts/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,36 @@ const app = new App({
});
```

## Sending log output somewhere besides the console
## Writing logs

The logger included with the constructed `App` can be used to log writings throughout your application code:

```javascript
(async () => {
app.logger.debug("Starting the app now!");
await app.start();
app.logger.info("⚡️ Bolt app started");
})();
```

Different app listeners can use the same `logger` that's provided as an argument to output additional details:

```javascript
app.event("team_join", async ({ client, event, logger }) => {
logger.info("Someone new just joined the team.");
try {
const result = await client.chat.postMessage({
channel: "C0123456789",
text: `Welcome to the team, <@${event.user.id}>!`,
});
logger.debug(result);
} catch (error) {
logger.error(error);
}
});
```

## Redirecting outputs

If you want to send logs to somewhere besides the console or want more control over the logger, you can implement a custom logger. A custom logger must implement specific methods (known as the `Logger` interface):

Expand Down
2 changes: 1 addition & 1 deletion docs/content/concepts/receiver.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ app.use(async ({ logger, context, next }) => {
// Start your app
await app.start(process.env.PORT || 3000);

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand Down
4 changes: 2 additions & 2 deletions docs/content/concepts/socket-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const app = new App({

(async () => {
await app.start();
console.log('⚡️ Bolt app started');
app.logger.info('⚡️ Bolt app started');
})();
```

Expand Down Expand Up @@ -48,6 +48,6 @@ const app = new App({

(async () => {
await app.start();
console.log('⚡️ Bolt app started');
app.logger.info('⚡️ Bolt app started');
})();
```
12 changes: 6 additions & 6 deletions docs/content/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const app = new App({
// Start your app
await app.start(process.env.PORT || 3000);

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand Down Expand Up @@ -238,7 +238,7 @@ app.message('hello', async ({ message, say }) => {
// Start your app
await app.start();

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand All @@ -263,7 +263,7 @@ app.message('hello', async ({ message, say }) => {
// Start your app
await app.start(process.env.PORT || 3000);

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand Down Expand Up @@ -350,7 +350,7 @@ app.message('hello', async ({ message, say }) => {
// Start your app
await app.start();

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand Down Expand Up @@ -394,7 +394,7 @@ app.message('hello', async ({ message, say }) => {
// Start your app
await app.start(process.env.PORT || 3000);

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```
</TabItem>
Expand Down Expand Up @@ -468,7 +468,7 @@ app.action('button_click', async ({ body, ack, say }) => {
// Start your app
await app.start(process.env.PORT || 3000);

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand Down
1 change: 1 addition & 0 deletions docs/content/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Listener functions have access to a set of arguments that may change based on th
| `respond` | `action`, `shortcut`, `view`, `command` | Function that responds to an incoming event **if** it contains a `response_url`. `respond` returns a promise that resolves with the results of responding using the `response_url`. For shortcuts, `respond` will **only** work for message shortcuts (not global shortcuts). For views, `respond` will **only** work when using `response_url_enabled: true` for [conversations list](https://api.slack.com/reference/block-kit/block-elements#conversation_select) and [channels list](https://api.slack.com/reference/block-kit/block-elements#channel_select) select menus in input blocks in modals. |
| `context` | All listeners | Event context. This object contains data about the event and the app, such as the `botId`. Middleware can add additional context before the event is passed to listeners. |
| `body` | All listeners | Object that contains the entire body of the request (superset of `payload`). Some accessory data is only available outside of the payload (such as `trigger_id` and `authorizations`). |
| `logger` | All listeners | The application logger with all of [the logging functions](/concepts/logging) for output. |

#### Body and payload references
The structure of the `payload` and `body` is detailed on the API site:
Expand Down
12 changes: 6 additions & 6 deletions docs/content/tutorials/ai-assistant.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ In this sample app, we've opted to rely on the thread context information provid
The [`assistant_thread_started`](https://api.slack.com/events/assistant_thread_started) event is sent when a user opens the assistant container, either with a DM or from the top nav bar entry point. Responding to this event starts the conversation with the user. Here we will greet the user then set some suggested prompts. The `message` field of each prompt is what is sent to the assistant when the user clicks on the prompt.

```js
threadStarted: async ({ event, say, setSuggestedPrompts, saveThreadContext }) => {
threadStarted: async ({ event, logger, say, setSuggestedPrompts, saveThreadContext }) => {
const { context } = event.assistant_thread;

try {
Expand Down Expand Up @@ -176,7 +176,7 @@ The [`assistant_thread_started`](https://api.slack.com/events/assistant_thread_s
*/
await setSuggestedPrompts({ prompts, title: 'Here are some suggested options:' });
} catch (e) {
console.error(e);
logger.error(e);
}
},
```
Expand All @@ -196,12 +196,12 @@ The [`assistant_thread_context_changed`](https://api.slack.com/events/assistant_
* method (either the DefaultAssistantContextStore or custom, if provided).
* https://api.slack.com/events/assistant_thread_context_changed
*/
threadContextChanged: async ({ saveThreadContext }) => {
threadContextChanged: async ({ logger, saveThreadContext }) => {
// const { channel_id, thread_ts, context: assistantContext } = event.assistant_thread;
try {
await saveThreadContext();
} catch (e) {
console.error(e);
logger.error(e);
}
},
```
Expand Down Expand Up @@ -272,7 +272,7 @@ For this scenario, the user is in a channel and the app has access to that chann
limit: 50,
});
} else {
console.error(e);
logger.error(e);
}
}
```
Expand Down Expand Up @@ -359,7 +359,7 @@ After getting the thread replies, we map them to the appropriate object structur
// Provide a response to the user
await say({ text: llmResponse.choices[0].message.content });
} catch (e) {
console.error(e);
logger.error(e);

// Send message to advise user and clear processing status if a failure occurs
await say({ text: 'Sorry, something went wrong!' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const app = new App({

(async () => {
await app.start();
console.log('⚡️ Bolt app started');
app.logger.info('⚡️ Bolt app started');
})();
```

Expand All @@ -71,7 +71,7 @@ app.event('message', async ({ event, client }) => {
});

receiver.router.use((req, res, next) => {
console.log(`Request time: ${Date.now()}`);
app.logger.info(`Request time: ${Date.now()}`);
next();
});

Expand All @@ -83,6 +83,6 @@ receiver.router.post('/secret-page', (req, res) => {

(async () => {
await app.start();
console.log('⚡️ Bolt app started');
app.logger.info('⚡️ Bolt app started');
})();
```
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const app = new App({
// init() メソッドを呼び出したので、`start()` メソッドを安全に呼び出すことができる
await app.start(process.env.PORT || 3000);
} catch (e) {
console.log(e);
app.logger.error(e);
process.exit(1);
}
})()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const app = new App({
// より一般的なグローバルのエラーハンドラー
app.error(async (error) => {
// メッセージ送信をリトライすべきか、アプリを停止すべきか判断するためにエラーの詳細を確認
console.error(error);
app.logger.error(error);
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const app = new App({

(async () => {
await app.start();
console.log('⚡️ Bolt app started');
app.logger.info('⚡️ Bolt app started');
})();
```

Expand Down Expand Up @@ -48,6 +48,6 @@ const app = new App({

(async () => {
await app.start();
console.log('⚡️ Bolt app started');
app.logger.info('⚡️ Bolt app started');
})();
```
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const app = new App({
// アプリを起動します
await app.start(process.env.PORT || 3000);

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand Down Expand Up @@ -250,7 +250,7 @@ app.message('hello', async ({ message, say }) => {
// アプリを起動します
await app.start();

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand All @@ -275,7 +275,7 @@ app.message('hello', async ({ message, say }) => {
// アプリを起動します
await app.start(process.env.PORT || 3000);

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand Down Expand Up @@ -366,7 +366,7 @@ app.message('hello', async ({ message, say }) => {
// アプリを起動します
await app.start();

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand Down Expand Up @@ -410,7 +410,7 @@ app.message('hello', async ({ message, say }) => {
// アプリを起動します
await app.start(process.env.PORT || 3000);

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand Down Expand Up @@ -482,7 +482,7 @@ app.action('button_click', async ({ body, ack, say }) => {
// アプリを起動します
await app.start();

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand Down Expand Up @@ -533,7 +533,7 @@ app.action('button_click', async ({ body, ack, say }) => {
// アプリを起動します
await app.start(process.env.PORT || 3000);

console.log('⚡️ Bolt app is running!');
app.logger.info('⚡️ Bolt app is running!');
})();
```

Expand Down
2 changes: 1 addition & 1 deletion src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export default class App<AppCustomContext extends StringIndexed = StringIndexed>
private receiver: Receiver;

/** Logger */
private logger: Logger;
public logger: Logger;

/** Log Level */
private logLevel: LogLevel;
Expand Down

0 comments on commit b6caa0a

Please sign in to comment.