Logging inside a celery task

I think we’ve all come upon this before, but when you want to debug a celery task in prod, it’s annoying because


    import logging


    def ….
        logging.error(“Something bad happened”)


does absolutely nothing! It turns out that celery has its own wrapper for the logging library that you have to use to make it work. All you have to do to make it work is replace `import logging` with


    from celery.log import get_task_logger
    logging = get_task_logger()


This logger will be named ‘celery.task.default’. I’ve added an entry to the settings.LOGGING for ‘celery.task’, so these will now get logged correctly.



Similar Articles

The World's Most Powerful Mobile Data Collection Platform

Start a FREE 30-day CommCare trial today. No credit card required.

Get Started

Learn More

Get the latest news delivered
straight to your inbox