Question

Second Edit: After a bit of digging, the question changed from how to log an exception with local variables to how to prevent celery from sending the 2nd log message which does not has the local vars. After the below attempt, I actually noticed that I was always receiving 2 emails, one with local vars per frame and the other without.

First Edit: I've managed to sort of get the local variables, by adding a custom on_failure override (using annotations for all tasks like so:

def include_f_locals(self, exc, task_id, args, kwargs, einfo):
    import logging
    logger = logging.getLogger('celery')
    logger.error(exc, exc_info=einfo)

CELERY_ANNOTATIONS = {'*': {'on_failure': include_f_locals}}

But the problem now is that the error arrives 3 times, once through the celery logger and twice through root (although I'm not propagating 'celery' logger in my logging settings)

Original question: I have a django/celery project to which I recently added a sentry handler as the root logger with level 'ERROR'. This works fine for most errors and exceptions occurring in django, except the ones coming from celery workers.

What happens is that sentry receives an exception with the traceback and the locals of the daemon, but does not include the f_locals (local vars) of each frame in the stack. And these do appear in normal python/django exceptions.

I guess I could try to catch all exceptions and log with exc_info manually. But this is less than ideal.

Was it helpful?

Solution

Funny enough, all my troubles went away when I simply upgraded raven to a version after 5.0 (specifically after 5.1).

While I'm not sure which changes caused exceptions to be logged properly (with f_locals correctly appearing in sentry), but the fact remains that raven < 5.0 did not work for me.

Also, there is no need to do any fancy CELERY_ANNOTATIONS legwork (as above), simply defining sentry handler for the correct logger seems to be enough to catch exceptions, as well as message of other logging levels.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top