【问题标题】:logging to file with twistd and python logging使用 twistd 和 python 日志记录到文件
【发布时间】:2015-07-11 20:27:46
【问题描述】:

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

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

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

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

这是我在 .tac 文件中初始化 python 日志记录的方式:

import logging

LOG_LEVEL = logging.DEBUG

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

【问题讨论】:

    标签: python ubuntu logging twisted


    【解决方案1】:

    使用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
    >>> 
    

    【讨论】:

    • 谢谢,这很好。请注意,也可以通过设置“stream=StreamHandler(stream=logfile).stream”在 basicConfig 中进行配置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 2018-09-08
    • 2017-04-13
    • 2015-09-19
    • 2015-04-26
    • 2011-06-12
    相关资源
    最近更新 更多