Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sentry logging in Django/Celery stopped working

I have no idea whats wrong. So far logging worked fine (and I was relying on that) but it seems to have stopped. I wrote a little test function (which does not work either):

core.tasks.py

import logging
from celery.utils.log import get_task_logger

logger = get_task_logger(__name__)
logger.setLevel(logging.DEBUG)

@app.task
def log_error():
    logger.error('ERROR')

settings.py

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

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {
        'level': 'INFO', #If set to DEBUG, prints ALL DJANGO debug logs.
        'handlers': ['console', 'sentry'],
    },
    'formatters': {
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        #log everything to the console
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
            'formatter': 'simple'
        },
        #logs directly to sentry
        'sentry': {
            'level': 'ERROR',
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
            #'class': 'raven.contrib.django.handlers.SentryHandler', #I have tried both
        },
    },
    'loggers': {
        'django.request':{
            'handlers': ['console', 'sentry'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'celery.task':{
            'handlers': ['console', 'sentry'],
            'level': 'DEBUG',
            'propagate': False,
        },
        #this is the logger for celery itself
        'celery':{
            'handlers': ['console', 'sentry'],
            'level': 'ERROR',
            'propagate': False,
        },
    },
}

from logging.config import dictConfig
dictConfig(LOGGING)

Executing the following in the Django shell logs to the console but it doesn't reach Sentry:

from core import tasks
tasks.log_error.delay()

It works when executing the task synchronously:

tasks.log_error()

Help!?

Django==1.6.2, raven==5.0.0, sentry==6.3.2, Python 2.7.3

like image 468
kev Avatar asked Jan 10 '23 00:01

kev


1 Answers

I had to add

CELERYD_HIJACK_ROOT_LOGGER=False

to my Django settings.py.

I don't really understand why I explicitely have to tell celery to not hijack the root logger.

like image 157
kev Avatar answered Jan 16 '23 20:01

kev