Sentry (Raven) with Django and Celery

Sentry is an error logging and aggregation platform, Raven is its client which supports Python, Ruby, PHP, Go, JavaScript, etc.

ref:
https://app.getsentry.com/docs/platforms/

Installation

First, create an account on https://getsentry.com/.

$ pip install raven

Configuration

in settings.py

INSTALLED_APPS = (
    ...
    'raven.contrib.django.raven_compat',
    ...
)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        },
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s',
        },
        'simple': {
            'format': '%(levelname)s %(name)s %(message)s',
        },
        'clear': {
            'format': '%(message)s',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'clear',
            'filters': ['require_debug_true', ],
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': ['require_debug_false', ],
        },
        'sentry': {
            'level': 'WARNING',
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
        },
    },
    'loggers': {
        'celery.worker': {
            'level': 'WARNING',
            'handlers': ['sentry', ],
        },
        'django.request': {
            'level': 'WARNING',
            'handlers': ['console', ],
        },
        'log_to_console': {
            'level': 'DEBUG',
            'handlers': ['console', ],
        },
        'log_to_sentry': {
            'level': 'WARNING',
            'handlers': ['sentry', ],
        },
        'raven': {
            'level': 'INFO',
            'handlers': ['console', ],
        },
        'sentry.errors': {
            'level': 'INFO',
            'handlers': ['console', ],
        },
    },
}

CELERYD_HIJACK_ROOT_LOGGER = False

in settings_prod.py

RAVEN_CONFIG = {
    'dsn': 'https://xxx:[email protected]/123',
}

LOGGING['root'] = {
    'level': 'WARNING',
    'handlers': ['sentry', ],
}

in settings_dev.py

RAVEN_CONFIG = {
    'dsn': '',
}

Usage

raven.py

import logging


# send a log to Sentry manually
sentry_logger = logging.getLogger('log_to_sentry')

try:
    do_something()
except:
    # If you're actually catching an exception, use `exc_info=True`
    sentry_logger.warning('TEST 1', exc_info=True)

# If you don't have an exception, but still want to capture a stacktrace, use the `stack` arg
sentry_logger.warning('TEST 2', extra={'stack': True})

# with extra information
logger.warning('TEST 3', extra={
    'stack': True,
    'data': {
        'your_data': 'any',
    },
})

raven.js

<script src="//cdnjs.cloudflare.com/ajax/libs/raven.js/x.x.x/raven.min.js"></script>
<script>
var options = {
  logger: 'js',
  ignoreUrls: [
    /graph\.facebook\.com/i
  ],
  includePaths: [
    /https?:\/\/(www\.)?getsentry\.com/,
    /https?:\/\/d3nslu0hdya83q\.cloudfront\.net/
  ]
};
Raven.config('https://[email protected]/123', options).install();
</script>
// record a simple message
Raven.captureMessage('Hello World!')

// capture an exception
try {
    errorThrowingCode();
} catch(err) {
    Raven.captureException(err);
}

ref:
http://raven-js.readthedocs.org/en/latest/
http://raven-js.readthedocs.org/en/latest/usage/index.html