【问题标题】:Python error log locationPython 错误日志位置
【发布时间】:2014-04-15 13:54:12
【问题描述】:

我正在flask 框架上开发一个python 应用程序。我使用.wsgi 来部署它。我对错误日志位置感到困惑。看起来 Python 错误和调试信息被放入了不同的日志文件中。

首先,我在apache vhost 文件中指定了访问和错误日​​志的位置。

<VirtualHost *:myport>
    ...
    CustomLog /homedir/access.log common
    ErrorLog /homedir/error.log
    ...
</VirtualHost>

我还知道还有另一个 apache 错误日志,/var/log/httpd/error_log。我的访问日志打印到了正确的位置,/homedir/access.log

但是,错误日志看起来很奇怪。 Python 错误,如SyntaxError,未捕获的异常进入vhost 文件(/homedir/error.log)中指定的日志文件。但是,python logging 模块生成的任何日志消息都会进入“默认”错误日志(/var/log/httpd/error_log)。

谁能解释为什么会发生这种情况以及如何解决它?我希望这两个日志去同一个地方。


感谢 cmets。这是我设置记录器的代码

import logging

# I didn't specify the stream, so it suppose to be stderr
# Don't know why stderr points to apache /var/log/*, rather than my error log

stream_handler = logging.StreamHandler()
formatter = logging.Formatter("%(levelname)s : %(pathname)s:%(lineno)s - %(msg)s")
stream_handler.setFormatter(formatter)

logger = logging.getLogger('foo')
logger.addHandler(stream_handler)
logger.setLevel(logging.DEBUG)

然后,我以这种方式记录消息。

import logging
logger = logging.getLogger('foo.bar')

logger.debug('Some debug information')

但是,日志转到 /var/log/httpd/error_log,而不是 apache vhost 文件中指定的日志。

【问题讨论】:

  • 也许它确实解决了您的问题,但您的 vhost 文件中有错字(或仅在给定的代码 sn-p 中):VirutalHostVirtualHost 不同
  • @Michael 对此感到抱歉。刚刚更正了。
  • 你是如何在你的烧瓶应用程序中初始化日志模块和级别的,你是如何调用日志方法的?
  • @bwbrowning 我刚刚编辑了问题,包括更多信息。谢谢。

标签: python apache logging error-handling flask


【解决方案1】:

你应该使用flask.logging.default_handler而不是logging.StreamHandler(),因为前者使用request.environ['wsgi.errors']创建流但后者没有,请参阅here了解详细信息。

有关request.environ['wsgi.errors'] 的更多信息,请参阅here

【讨论】:

    猜你喜欢
    • 2013-10-19
    • 2013-10-15
    • 1970-01-01
    • 2017-03-19
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 2013-01-02
    相关资源
    最近更新 更多