Slackbot
11/03/2023, 6:47 PMMarvin
11/03/2023, 6:48 PMlogging.yml
file. This file contains the existing log handlers: the console
handler, and the prefect server
handler that sends logs to the API. You can create a new handler that catches exceptions and sends them to Slack. Here is an example of what the handlers
section might look like:
yaml
handlers:
console:
level: 0
class: prefect.logging.handlers.PrefectConsoleHandler
formatter: standard
styles:
# styles omitted for brevity
prefect server:
level: 0
class: prefect.logging.handlers.PrefectServerHandler
slack:
level: 0
class: YourSlackHandlerClass # replace with your Slack handler class
formatter: standard
2. Configure a notification in your Prefect server. Go to the Notifications page and select Create Notification or the + button. Notifications are structured just as you would describe them to someone. You can choose which run states should trigger a notification, tags to filter which flow runs are covered by the notification, and whether to send an email, a Slack message, Microsoft Teams message, or other services.
For Slack notifications, the configuration requires webhook credentials for your Slack and the channel to which the message is sent. For example, to get a Slack message if a flow with a daily-etl
tag fails, the notification will read: "If a run of daily-etl
fails, send a Slack message to #channel
."
Please note that you will need to implement the Slack handler class (YourSlackHandlerClass
in the example above) yourself. This class should inherit from the base logging.Handler class in Python's logging module, and override the emit
method to send a message to Slack whenever it receives a log record.
For more information, you can refer to these resources:
- How to stream prefect logs to a file
- Prefect Notifications Guide