Skip to main content

Python exception notifier for Airbrake

Project description

Python exception notifier for Airbrake

Build Status

Installation

pybrake requires Python 3.4+.

pip install -U pybrake

Configuration

To configure the pybrake notifier you will need your Airbrake project's id and api_key, these are available from your project's settings page.

import pybrake


notifier = pybrake.Notifier(project_id=123,
                            project_key='FIXME',
                            environment='production')

Sending errors to Airbrake

try:
    raise ValueError('hello')
except Exception as err:
    notifier.notify(err)

Sending errors synchronously

By default, the notify function sends errors asynchronously using ThreadPoolExecutor and returns a concurrent.futures.Future, a synchronous API is also made available with the notify_sync function:

notice = notifier.notify_sync(err)
if 'id' in notice:
    print(notice['id'])
else:
    print(notice['error'])

Adding custom params

To set custom params you can build and send notice in separate steps:

notice = notifier.build_notice(err)
notice['params']['myparam'] = 'myvalue'
notifier.send_notice(notice)

You can also add custom params to every error notice before it's sent to Airbrake with the add_filter function.

def my_filter(notice):
    notice['params']['myparam'] = 'myvalue'
    return notice

notifier.add_filter(my_filter)

Ignoring notices

There may be some notices/errors thrown in your application that you're not interested in sending to Airbrake, you can ignore these using the add_filter function.

def my_filter(notice):
    if notice['context']['environment'] == 'development':
        # Ignore notices in development environment.
        return None
    return notice

notifier.add_filter(my_filter)

Logging integration

pybrake provides a logging handler that sends your logs to Airbrake.

import logging
import pybrake


airbrake_handler = pybrake.LoggingHandler(notifier=notifier,
                                          level=logging.ERROR)

logger = logging.getLogger('test')
logger.addHandler(airbrake_handler)

logger.error('something bad happened')

Django integration

First you need to add your pybrake config to your Django settings.py file using your project's id and api_key.

AIRBRAKE = dict(
    project_id=123,
    project_key='FIXME',
)

The next step is activating the Airbrake middleware.

MIDDLEWARE = [
    ...
    'pybrake.django.AirbrakeMiddleware',
]

The last step is configuring the airbrake logging handler. After that you are ready to start reporting errors to Airbrake from your Django app.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'airbrake': {
            'level': 'ERROR',
            'class': 'pybrake.LoggingHandler',
        },
    },
    'loggers': {
        'app': {
            'handlers': ['airbrake'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}

Flask integration

The Flask integration leverages Flask signals and therefore requires the blinker library.

from flask import Flask
import pybrake.flask



app = Flask(__name__)

app.config['PYBRAKE'] = dict(
    project_id=123,
    project_key='FIXME',
)
app = pybrake.flask.init_app(app)

Disabling pybrake logs

The pybrake logger can be silenced by setting the logging level to logging.CRITICAL.

import logging


logging.getLogger("pybrake").setLevel(logging.CRITICAL)

Development

Running the tests

pip install -r test-requirements.txt
pytest

Uploading to PyPI

python setup.py sdist upload

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybrake-0.3.2.tar.gz (11.4 kB view details)

Uploaded Source

File details

Details for the file pybrake-0.3.2.tar.gz.

File metadata

  • Download URL: pybrake-0.3.2.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pybrake-0.3.2.tar.gz
Algorithm Hash digest
SHA256 cdcc9cb84b08b93dd428758b74af8fa56235ff8e0813630cb0ee1872cb8d9049
MD5 b8640ba5aa1a9ff8ca84973030eb1b8b
BLAKE2b-256 f5be0fc0d23c6592e0cb22d300de53e08b6fef1d0c626962ea9dcbb9ab159199

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page