apache Python错误日志位置

yruzcnhs  于 2022-11-16  发布在  Apache
关注(0)|答案(1)|浏览(128)

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

<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)。
有没有人能解释一下为什么会发生这种情况,以及如何修复它?我希望这两个日志去同一个地方。
谢谢你的评论。下面是我设置日志的代码

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文件中指定的日志。

vfwfrxfs

vfwfrxfs1#

你应该使用flask.logging.default_handler而不是logging.StreamHandler(),因为前者使用request.environ['wsgi.errors']来创建流,而后者没有,详细信息请参阅此处。
有关request.environ['wsgi.errors']的更多信息,请参见此处。

相关问题