Sending uncaught Python exceptions to Airbrake

Overview

This example shows you how to send uncaught exceptions in Python to Airbrake. For general instructions how to setup Airbrake with your Python application please visit the repository on GitHub here.

By overriding sys.excepthook we can send uncaught exceptions to Airbrake.

Example Code

import logging
import sys
import traceback
import airbrake

def f():
    return g()

def g():
    return h()

def h():
    return i()

def i():
    1/0

def log_uncaught_exceptions(ex_cls, ex, tb):
    logging.critical(''.join(traceback.format_tb(tb)))
    logging.critical('{0}: {1}'.format(ex_cls, ex))

if __name__ == '__main__':
    logging = airbrake.getLogger(api_key="******",
                                project_id=****,
                                environment="development")

    sys.excepthook = log_uncaught_exceptions

    logging.debug('About to do f().')

    try:
        f()
    except:
        pass

Having trouble? Email us at support@airbrake.io