Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Fix celery error logging #1267

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions raven/contrib/celery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

from celery.exceptions import SoftTimeLimitExceeded
from celery.signals import (
after_setup_logger, task_failure, task_prerun, task_postrun
)
after_setup_logger, after_setup_task_logger,
task_failure, task_prerun, task_postrun)
from raven.handlers.logging import SentryHandler


Expand Down Expand Up @@ -46,10 +46,34 @@ def process_logger_event(sender, logger, loglevel, logfile, format,
if isinstance(h, SentryHandler):
h.addFilter(filter_)
return False

logger.addHandler(handler)

after_setup_logger.connect(process_logger_event, weak=False)
def fix_task_logger(sender, logger=None, **kwargs):
# If the task logger is set up to not propagate (as Celery is bound
# to do), give it a SentryHandler from a parent logger. However, if
# no parent is using Sentry, the task logger should not, either.
if not logger or logger.propagate:
return
if any(isinstance(h, SentryHandler) for h in logger.handlers):
return
parent = logger.parent
while parent:
for handler in parent.handlers:
if isinstance(handler, SentryHandler):
logger.addHandler(handler)
return
parent = parent.parent

after_setup_logger.connect(
process_logger_event,
weak=False,
dispatch_uid='raven.contrib.celery.process_logger_event',
)
after_setup_task_logger.connect(
fix_task_logger,
weak=False,
dispatch_uid='raven.contrib.celery.fix_task_logger',
)


class SentryCeleryHandler(object):
Expand Down
15 changes: 6 additions & 9 deletions raven/contrib/django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,12 @@ def install_celery(self):
ignore_expected=ignore_expected)\
.install()

# try:
# ga = lambda x, d=None: getattr(settings, 'SENTRY_%s' % x, d)
# options = getattr(settings, 'RAVEN_CONFIG', {})
# loglevel = options.get('celery_loglevel',
# ga('CELERY_LOGLEVEL', logging.ERROR))

# register_logger_signal(client, loglevel=loglevel)
# except Exception:
# logger.exception('Failed to install Celery error handler')
loglevel = (
getattr(settings, 'RAVEN_CONFIG', {}).get('CELERY_LOGLEVEL')
or getattr(settings, 'SENTRY_CELERY_LOGLEVEL', None)
)
if loglevel is not None:
register_logger_signal(client, loglevel=loglevel)

def install(self):
request_started.connect(self.before_request, weak=False)
Expand Down