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

一个人想着一个人 提交于 2019-12-24 05:32:26

问题


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:


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)


来源:https://stackoverflow.com/questions/56673575/is-it-possible-to-control-the-datefmt-of-the-celery-task-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!