如何为eliottree render_tasks生成eliot任务?

时间:2017-03-21 19:02:48

标签: python logging

如何为tasks函数生成render_tasks参数(在eliottree docs中提及?)

import json, sys
from eliottree import tasks_from_iterable, render_tasks
# Or `codecs.getwriter('utf-8')(sys.stdout).write` on Python 2.
render_tasks(sys.stdout.write, tasks, colorize=True)

我认为应该以某种方式使用tasks_from_iterable,但它消耗的是什么?

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了。相反,to_file您必须将add_destination与功能对象一起使用,该功能对象将在以下情况下收到message Message.log()被称为:

from sys import stdout
from codecs import getwriter
from eliottree import tasks_from_iterable, render_tasks
from eliot import add_destination

add_destination(render_stdout)

def render_stdout(message):
    render_tasks(getwriter('utf-8')(stdout).write, tasks_from_iterable([message]), colorize=True, human_readable=True)

整个树不会在​​这里渲染,一次只能渲染一个分支,但这就是你生成艾略特任务的方式。