Kurt Peek
Kurt Peek

Reputation: 57461

How to use Airbrake's pybrake Django integration?

I'm trying to apply the Django integration steps described for pybrake, a Python exception notifier for Airbrake. The Django project contains several apps, one of which is called lucy_web. In my setttings, I've defined the AIRBRAKE setting like so:

AIRBRAKE = dict(
    project_id=os.getenv('AIRBRAKE_API_KEY'),
    project_key=os.getenv('AIRBRAKE_PROJECT_ID'),
    environment=os.getenv('AIRBRAKE_ENVIRONMENT', default='production'),
    root_directory=os.path.dirname(os.getcwd()))

The LOGGING setting is:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        }
    },
    'handlers': {
        'console': {
            'level': os.environ.get('LOG_LEVEL', 'ERROR'),
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
        },
        'airbrake': {
            'level': 'ERROR',
            'class': 'pybrake.LoggingHandler',
        }
    },
    'loggers': {
        'django.db.backends': {
            'level': os.environ.get('LOG_LEVEL', 'ERROR'),
            'handlers': ['console'],
        },
        'lucy_web': {
            'handlers': ['airbrake'],
            'level': 'ERROR',
            'propagate': True,
        }
    }
}

Note that I've replaced the key 'app' in the README by 'lucy_web', the name of one of the apps in the project.

Now, as I understand from Python's logging documentation (https://docs.python.org/3/library/logging.html), any Logger object with a name like 'lucy_web.foo.bar' should propagate to the 'lucy_web' logger and get handled by its handler, which is defined above to be the pybrake.LoggingHandler.

To test this, I tried the following test in lucy_web/tests/test_airbrake.py:

import os
import logging
import pybrake
from django.test import SimpleTestCase

logger = logging.getLogger(__name__)


class AirbrakeTest(SimpleTestCase):
    def test_1(self):
        notifier = pybrake.Notifier(
            project_id=os.getenv('AIRBRAKE_PROJECT_ID'),
            project_key=os.getenv('AIRBRAKE_PROJECT_KEY'),
            environment='production',
            root_directory=os.path.dirname(os.getcwd()))

        try:
            raise ValueError("Hello")
        except Exception as err:
            notifier.notify(err)

    def test_2(self):
        logger.error("Foobar")

The first test is a more 'verbose' approach in which a pybrake.Notifier is instantiated directly. This succeeds in sending an error to Airbrake, as seen from our Airbrake dashboard:

enter image description here

However, the second test, which relies on the logging integration, doesn't appear to send any notification to Airbrake. Is this not supposed to be how it is supposed to work?

Upvotes: 2

Views: 184

Answers (0)

Related Questions