Attention Pythonistas: Honeybadger now supports reporting exceptions from Python and Django applications!

It's true that IRL honey badgers may not get along with IRL pythons, but on the internets they are now the best of friends thanks to our new honeybadger-python client package. In this post we'll discuss some of the features we have already built, how to set everything up, and what we plan to add in the future.

Installing

To get started, install our package with pip:

$ pip install honeybadger

For Django

In a Django application, add the Honeybadger Django middleware to the top of your MIDDLEWARE_CLASSES config variable:

MIDDLEWARE_CLASSES = (
  'honeybadger.middleware.DjangoHoneybadgerMiddleware',
  ...
)

You'll also need to add a new HONEYBADGER config variable to your settings.py to specify your API key:

HONEYBADGER = {
  'API_KEY': 'myapikey'
}

That's it! We'll report all unhandled exceptions which happen during your Django web requests. We also automatically include a lot of useful request data, such as parameters and session information:

For other frameworks/plain Python apps

Django is the only explicitly supported framework at the moment. For other frameworks (Flask, web2py, etc.) or a plain Python script, simply import honeybadger and configure it with your API key. Honeybadger uses a global exception hook to automatically report any uncaught exceptions.

from honeybadger import honeybadger
honeybadger.configure(api_key='myapikey')

raise Exception, "This will get reported!"

Reporting exceptions manually

In some cases you may want to catch an exception using a try statement, in which case it won't be caught by the global exception handler. Instead, you can use the Honeybadger.notify method:

mydict = dict(a=1)
try:
  print mydict['b']
except KeyError, exc:
  honeybadger.notify(exc, context={'foo': 'bar'})

You can also use Honeybadger.notify to report an arbitrary error, without an exception object:

honeybadger.notify(error_class='ValueError', error_message='Something bad happened!')

We'll even automatically generate a traceback from the location the method is called.

Adding context

Now that Honeybadger is recording your exceptions it's time to think about context. Use Honeybadger.set_context to add extra contextual data which will be reported with any errors which occur in the current transaction. For example, let's say we want to track which user is currently logged in:

honeybadger.set_context(
  user_email='user@example.com',
  user_admin_url='https://admin.example.com/users/1'
 )

This data will be displayed in Honeybadger with every error that gets reported:

You can use honeybadger.reset_context to reset any context which has already been added and start fresh. In a Django web request we do this for you automatically.

If you don't want to set global context data, you can use our Python context manager to set case-specific contextual information:

# from a Django view
from honeybadger import honeybadger
def my_view(request):
  with honeybadger.context(user_email=request.POST.get('user_email', None)):
    form = UserForm(request.POST)
    # ...

Filtering sensitive data

In some cases you may not want to send certain types of sensitive request data to Honeybadger, such as passwords and credit cards. You can configure keys to exclude from the data that we collect using the params_filters config option:

honeybadger.configure(
  api_key='myapikey',
  params_filters=['super', 'secret', 'keys']
)

By default we filter out "password", "password_confirmation", and "credit_card" (you will need to include these in the list if you provide your own params_filters, though):

What's next?

This is the first release of Honeybadger for Python, so there's plenty of work to do!

Next up we want to add the ability to report error level logs from Python's logging framework, add support for more frameworks such as Flask, support sending request metrics/slow request traces, and more.

We'd also like to support more versions of Python and Django (the package is currently tested against Django 1.9 and Python 2.7).

Feedback is welcome! Have a comment, question, suggestion, or bug report? Email me, submit an issue, or send a pull request.

Need a Honeybadger account? Start your free trial and exterminate your Python errors today!

Get the Honeybadger newsletter

Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo
    Joshua Wood

    Josh started hacking on IRC scripts in the late nineties and has been hacking on computers (and businesses) ever since. As a co-founder of Honeybadger he works on the product, leads marketing and developer relations, and is involved in many open-source projects.

    More articles by Joshua Wood
    An advertisement for Honeybadger that reads 'Turn your logs into events.'

    "Splunk-like querying without having to sell my kidneys? nice"

    That’s a direct quote from someone who just saw Honeybadger Insights. It’s a bit like Papertrail or DataDog—but with just the good parts and a reasonable price tag.

    Best of all, Insights logging is available on our free tier as part of a comprehensive monitoring suite including error tracking, uptime monitoring, status pages, and more.

    Start logging for FREE
    Simple 5-minute setup — No credit card required