Other Python Apps

Add Bugsnag to other Python applications, such as standalone scripts.

Installation

Using PyPI:

pip install bugsnag

Basic configuration

Configure the package when your python app starts:

import bugsnag
bugsnag.configure(
  api_key = "YOUR_API_KEY_HERE",
  project_root = "/path/to/your/app",
)

Logging configuration

You can connect Bugsnag up to Python’s logging framework so that anything of level “error” or above is logged to Bugsnag.

Here is a plain Python example:

import logging

from bugsnag.handlers import BugsnagHandler

#call bugsnag.configure() here
logger = logging.getLogger("test.logger")
logger.addHandler(BugsnagHandler())

Reporting unhandled errors

At this point, Bugsnag should be installed and configured, and any unhandled exceptions will be automatically detected and should appear in your Bugsnag dashboard.

Reporting handled errors

If you would like to send handled exceptions to Bugsnag, you should import the bugsnag module:

import bugsnag

Then to notify Bugsnag of an error, you can call bugsnag.notify:

bugsnag.notify(Exception("Something broke!"))

You can also pass additional configuration options in as named parameters. These parameters will only affect the current call to notify.

Sending diagnostic data

The meta_data field is a dictionary of dictionaries which will be rendered as a tab in a Bugsnag error report. This example would create a special_info tab:

bugsnag.notify(Exception("Something broke!"),
    context="myContext",
    meta_data={"special_info":{"request_id": 12345, "message_id": 854}}
)

For more information, see reporting handled errors.

Logging diagnostic data

The BugsnagHandler accepts a special keyword argument to its __init__() function: extra_fields. This is optional and may be a dictionary of extra attributes to gather from each LogRecord and insert into meta_data to be attached to Bugsnag error reports.

The keys in the extra_fields dictionary should be tab names for where you would like the data displayed in Bugsnag, and the values should be attributes to pull off each log record and enter into section. The attributes are not required to exist on the log record, and any non-existent attribute will be ignored. Example:

bs_handler = BugsnagHandler(extra_fields={"some_tab":["context_attribute"]})

This is very useful if you are assigning context-specific attributes to your LogRecord objects, as described in the python logging cookbook.

Identifying users

The user field is information about the person currently using your application. It should be a dictionary containing id, email, and/or name.

bugsnag.notify(e, user={"id":"bob-hoskins", name: "Bob Hoskins", email: "foo@bar.com"})

For more information, see reporting handled errors. To set user information for every report, see customizing error reports.

Next steps