我正在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
)中指定的日志文件。但是,pythonlogging
模块生成的任何日志消息都会转到“默认”错误日志(/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文件中指定的日志。
您应该使用
flask.logging.default_handler
,而不是logging.StreamHandler()
,因为前者使用request.environ['wsgi.errors']
来创建流,但后者没有,请参阅here了解详细信息。有关
request.environ['wsgi.errors']
的更多信息,请参见here。相关问题 更多 >
编程相关推荐