6

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. 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?

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'),
    ...
4

2 回答 2

2

正如@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

更新

文档

您也可以使用print(), 因为任何写入标准 out/-err 的内容都将被重定向到日志系统(您可以禁用此功能,请参阅 参考资料 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'}
于 2015-11-13T10:31:00.990 回答
1

尝试使用 CELERYD_LOG_FORMAT

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

于 2015-11-13T09:59:10.347 回答