问题
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