我正在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
)。
有没有人能解释一下为什么会发生这种情况,以及如何修复它?我希望这两个日志去同一个地方。
谢谢你的评论。下面是我设置日志的代码
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文件中指定的日志。
1条答案
按热度按时间vfwfrxfs1#
你应该使用
flask.logging.default_handler
而不是logging.StreamHandler()
,因为前者使用request.environ['wsgi.errors']
来创建流,而后者没有,详细信息请参阅此处。有关
request.environ['wsgi.errors']
的更多信息,请参见此处。