Skip to content

Instantly share code, notes, and snippets.

@marteinn
Last active May 9, 2021 04:20
Show Gist options
  • Save marteinn/18bd85db1a908361858ab6976294680d to your computer and use it in GitHub Desktop.
Save marteinn/18bd85db1a908361858ab6976294680d to your computer and use it in GitHub Desktop.
Logging Flask Exceptions to Slack
  1. First off install the python package slacker-log-handler and add it to your requirements file.

    flask
    ...
    slacker-log-handler
  2. Generate a slack token.

  3. Add a SlackLogHandler instance to your app logger, I use the layout mentioned in the flask docs.

    if not app.debug:
        from slacker_log_handler import SlackerLogHandler
        slack_handler = SlackerLogHandler('YOUR-SLACK-TOKEN',
                                          'YOUR-SLACK-CHANNEL',
                                          stack_trace=True,
                                          username='USERNAME (like: site-bot)')
        slack_handler.setFormatter(Formatter(app.config['LOGGING_FORMAT']))
        file_handler.setLevel(logging.ERROR)
        app.logger.addHandler(slack_handler)
  4. To test your integration, open a flask shell and type something like this:

    from app import app
    app.logging.error("ERROR")

    ... A message should pop up in your the slack channel you specified.

  5. Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment