gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:28:18 27 4
gpt4 key购买 nike

当运行 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'),
...

最佳答案

正如@xbirkettx 提到的,输出格式在 CELERYD_LOG_FORMAT 设置中定义,默认为 [%(asctime)s: %(levelname)s/%(processName)s] % (消息)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'}

关于python - 如何从 celery pprint 输出中删除时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33548095/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com