Logging Session and Environment with airbrake-python

Filed in: Python

My Pull Request for airbrake-python to support logging session and environment details in Airbrake was merged recently. This has made it possible to easily log session and environment details using Python’s logging facility.


Let’s use pip to install the latest version of airbrake-python:

pip install -e git+git@github.com:airbrake/airbrake-python.git#egg=airbrake-python


The following environment variables must be set for these examples to work:

  • AIRBRAKE_API_KEY the project API key located in the project settings
  • AIRBRAKE_PROJECT_ID the project ID located in the URL project_id parameter

Simple Logging

It’s very easy to up and running with airbrake-python. The following program will log a simple error message:

import airbrake
logger = airbrake.getLogger('logthethings')
logger.exception("This is an error")

This program logs an error in Airbrake and displayed as follows:

Airbrake Simple Error

Logging Session Details

To include session details, we need to include a session value in the extra parameter:

session = {'user': 'brembo', 'email': 'brembo@example.com'}
extra = {'session': session}
    "This is an error with session details", extra=extra)

Airbrake displays an additional Session tab for this error:

Airbrake Session Error

Logging Environment Details

Including environment details follows the same pattern:

logger = airbrake.getLogger('logthethings')
extra = {'environment': dict(os.environ)}
    "This is an error with environment details", extra=extra)

Airbrake now displays the Environment tab for this error:

Airbrake Environment Error

That’s all folks!

And that’s all there is to it. Happy logging!

Thanks to Sam Stavinoha for dealing with my PRs in double-quick time.