如何从 celery pprint 输出中删除时间戳?

How to remove timestamps from celery pprint output?

当运行 celery worker 时,pprint 输出的每一行总是以时间戳为前缀并且也被剥离。这使得它很难读:

[2015-11-05 16:01:12,122: WARNING/Worker-2] {
[2015-11-05 16:01:12,122: WARNING/Worker-2] u'key1'
[2015-11-05 16:01:12,122: WARNING/Worker-2] :
[2015-11-05 16:01:12,122: WARNING/Worker-2] 'value1'
[2015-11-05 16:01:12,122: WARNING/Worker-2] ,
u'_id':
[2015-11-05 16:01:12,122: WARNING/Worker-2] ObjectId('55fff3b74322c53d18ae4687')
...

有没有办法告诉 celery 不要格式化 pprint 的输出?

更新:

问题放错了。所需的输出看起来像这样:

[2015-11-05 16:01:12,122: WARNING/Worker-2] 
{
    u'key1': 'value1',
    u'_id': ObjectId('55fff3b74322c53d18ae4687'),
    ...

试试 CELERYD_LOG_FORMAT

http://docs.celeryproject.org/en/latest/configuration.html

如@xbirkettx 所述,输出格式在 CELERYD_LOG_FORMAT 设置中定义,默认为 [%(asctime)s: %(levelname)s/%(processName)s] %(message)s.

因此,在您的设置中:

CELERYD_LOG_FORMAT = '[%(levelname)s/%(processName)s] %(message)s'

任务中记录器还有一个特殊设置,默认为:

CELERYD_TASK_LOG_FORMAT = [%(asctime)s: %(levelname)s/%(processName)s]
[%(task_name)s(%(task_id)s)] %(message)s

删除 asctime 键以删除时间戳。 CELERYD_TASK_LOG_FORMAT.

上的文档

更新

来自docs

You can also use print(), as anything written to standard out/-err will be redirected to the logging system (you can disable this, see CELERY_REDIRECT_STDOUTS).

所以,与其调用pprint.pprint,不如用pprint.pformat格式化一个字符串,然后记录它。

@periodic_task(run_every=timedelta(seconds=10))
def pprint_dict2():
    import pprint
    values = {
        u'key1': 'value1',
        u'_id1': "ObjectId('55fff3b74322c53d18ae4687')",
        u'key2': 'value2',
        u'_id2': "ObjectId('55fff3b74322c53d18ae4687')",
        u'key3': 'value3',
        u'_id3': "ObjectId('55fff3b74322c53d18ae4687')",
        u'key4': 'value4',
        u'_id3': "ObjectId('55fff3b74322c53d18ae4687')",
    }
    s = pprint.pformat(values, width=1)
    print(s)  # or even better logger.info(...)

输出:

[WARNING/Beat] {u'_id1': "ObjectId('55fff3b74322c53d18ae4687')",
 u'_id2': "ObjectId('55fff3b74322c53d18ae4687')",
 u'_id3': "ObjectId('55fff3b74322c53d18ae4687')",
 u'key1': 'value1',
 u'key2': 'value2',
 u'key3': 'value3',
 u'key4': 'value4'}