1

我正在尝试守护一个使用 Python2.7 标准库记录器的简单 Twisted 应用程序。

这是设置:

Python 应用程序文件(run.py):

import logging
import logging.config
from twisted.internet import reactor

import conf

def main():
    logging.config.dictConfig(conf.LOGGERCONF)
    logger = logging.getLogger(conf.LOGGERNAME)
    logger.debug('Starting the reactor...')
    reactor.callWhenRunning(lambda: logger.info('Reactor started'))
    reactor.run()

main()

Twistd 应用程序文件(daemon.tac):

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

application = Application('run')
application.setComponent(ILogObserver, PythonLoggingObserver().emit)

日志观察器文件(daemonlog.py):

from twisted.python import log

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

启动应用程序:

python run.py

一切正常,日志消息被正确归档和显示(根据记录器配置)。

尝试使用以下方式进行守护进程:

twistd -y daemon.tac --logger=daemonlog.logger

守护程序启动正常,我可以看到创建的 twistd.pid 文件(具有正确的 pid 号),在 ps -ef 命令结果中运行的守护程序,但我看不到任何日志文件(twistd.log 或日志文件由应用程序在没有扭曲的情况下正常启动时创建的)。

最后,我只想使用 Python 的标准库记录器和一种“绕过”twistd 记录器。

我可能遗漏了一些东西或误解,任何帮助将不胜感激。

4

0 回答 0