Is it possible to control the datefmt of the Celery task logs?

Viewed 262

With normal python loggers you might configure the format string and date format like so:

{                                                                                                     
    'format': '%(asctime)s.%(msecs)d [%(process)d] %(levelname)s [%(name)s] %(message)s',                       
    'datefmt': %d/%m/%Y-%H:%M:%S'                                                                              
}

But I am completely unable to find any way to pass a datefmt to the Celery task logger. It seems like such a basic bit of functionality that I'm surprised if it is impossible.

The closest I got was by following the technique layed out in this article. The gist of it is to use the after_setup_task_logger signal to customize the logger handler to pass a custom TaskFormatter. But the trick is that while TaskFormatter let's you pass a format string, it doesn't seem to have any provision to allow you to pass a datefmt.

This seems to be confirmed by this open issue on the Celery github.

So is there something obvious I'm missing? A trick that can be done without having to wait for celery to handle the above issue? Is it really impossible?

1 Answers

It's possible to set the datefmt after the task formatter has been created by setting the datefmt attribute. The __init__ of the Formatter is doing just that.

tf = TaskFormatter('%(asctime)s.%(msecs)d [%(process)d] %(levelname)s [%(name)s] %(message)s')
tf.datefmt = '%d/%m/%Y-%H:%M:%S' 
handler.setFormatter(tf)
Related