使用 twistd 和 python 记录到文件

logging to file with twistd and python logging

当我 运行 我扭曲的应用程序是这样的:

twistd --pidfile ./twistd.pid -l $HOME/logs/my_application.log -oy service.tac

我发现它不会记录通过 python logging 系统发送的任何内容。我注意到在 twisted 中有一个 "PythonLoggingObserver",但是它会将 twisted 日志重定向到日志记录模块。

我想知道如何让所有日志(不管日志模块)转到在 twistd 上用“-l”命令指定的日志文件。我怀疑我的日志正在写入守护程序的标准输出。

以下是我如何初始化 python 登录 .tac 文件:

import logging

LOG_LEVEL = logging.DEBUG

logger = logging.getLogger(module_name)
logger.setLevel(LOG_LEVEL)
logging.basicConfig(level=LOG_LEVEL)

使用 twisted.python.log.logfile 添加流处理程序。例如:

>>> from sys import stdout
>>> from logging import StreamHandler, getLogger
>>> from twisted.python.log import startLogging, logfile
>>> observer = startLogging(stdout, setStdout=False)
2015-05-02 06:34:39-0400 [-] Log opened.
>>> getLogger().addHandler(StreamHandler(stream=logfile))
>>> getLogger().log(100, "Hello")
2015-05-02 06:36:26-0400 [-] Hello
>>>