如何在使用IPlugin时设置twistd.py ILogObserver?

时间:2013-03-05 16:36:57

标签: python logging twisted twistd

我想将twistd.py的日志记录重定向到python的日志记录。正常启动.tac文件时,我可以轻松地执行此操作:

from twisted.python.log import PythonLoggingObserver, ILogObserver
from twisted.application import service

application = service.Application("FooApp")
application.setComponent(ILogObserver, PythonLoggingObserver().emit)

但是,在撰写Application时,我似乎没有IPlugin。相反,我只有一个实现IServiceMakerIPlugin的类,其中makeService返回service.Service。如何设置此日志观察器?

请注意,我不只是想添加一个python日志记录观察器,我想重定向 twistd的日志记录,以便它只通过python的内置日志记录系统。

1 个答案:

答案 0 :(得分:3)

查看twistd' --logger参数:

# mylogger.py
from twisted.python import log

def logger():
    return log.PythonLoggingObserver().emit

然后调用twistd:twistd --logger=mylogger.logger

相关问题