Sentry logging in Django/Celery stopped working

核能气质少年 提交于 2019-12-07 10:30:01

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/25320200/sentry-logging-in-django-celery-stopped-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!