简体   繁体   English

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

[英]How to remove timestamps from celery pprint output?

When running the celery worker then each line of the output of the pprint is always prefixed by the timestamp and also is being stripped. 当运行芹菜工作者时, pprint输出的每一行总是以时间戳为前缀并且也被剥离。 This makes it quite unreadable: 这使得它非常难以理解:

[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')
...

Is there a way how to tell celery not to format the output of pprint ? 有没有办法告诉芹菜不要格式化pprint的输出?

UPDATE: 更新:

the question was placed a bit wrong. 这个问题有点不对劲。 The desired output would look something like this: 所需的输出看起来像这样:

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

As @xbirkettx mentioned the output format is deffined in the CELERYD_LOG_FORMAT setting, which defaults to [%(asctime)s: %(levelname)s/%(processName)s] %(message)s . 正如@xbirkettx所提到的那样,输出格式在CELERYD_LOG_FORMAT设置中定义,默认为[%(asctime)s: %(levelname)s/%(processName)s] %(message)s

So, in your settings: 所以,在你的设置中:

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

There's also a special setting for a in-task logger, which defaults to: 任务记录器还有一个特殊设置,默认为:

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

Remove the asctime key to get rid of the timestamp. 删除asctime键以删除时间戳。 Docs on CELERYD_TASK_LOG_FORMAT . 关于CELERYD_TASK_LOG_FORMAT文档。

Update 更新

From the 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 ). 您也可以使用print() ,因为写入标准输出/ -err的任何内容都将被重定向到日志记录系统(您可以禁用它,请参阅CELERY_REDIRECT_STDOUTS )。

So, rather than calling pprint.pprint , it is better to format a string with pprint.pformat and then log it. 因此,不是调用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(...)

Output: 输出:

[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'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM