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

Usage

Creating notifier:

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)

By default notify sends errors asynchronously using ThreadPoolExecutor and returns a concurrent.futures.Future, but synchronous API is also available:

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

You can also set custom params on all reported notices:

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

notifier.add_filter(my_filter)

Or ignore notices:

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 provide 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 pybrake config to your Django settings.py file:

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

Then you can activate Airbrake middleware:

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

And configure logging handler:

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

Flask integration

Flask integration uses Flask signals and therefore requires 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

pybrake logger can be silenced using following code:

import logging


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

Development

Run tests:

pip install -r test-requirements.txt
pytest

Upload 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.1.tar.gz (11.2 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for pybrake-0.3.1.tar.gz
Algorithm Hash digest
SHA256 828df7b2d25769ad24c98036e74cf1e96a62d0fbb5e255c5abebe79e178ccec7
MD5 136a7b249efebce9a4c22992b0392c79
BLAKE2b-256 1c9a2c3520ad746f23719fa82db3431863b3bd069d4834bccdf0413f5dd1d705

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