Python的mod_wsgi的写入自定义日志文件而不是Apache的error_log中 [英] Python mod_wsgi write to custom log file instead of apache error_log

查看:907
本文介绍了Python的mod_wsgi的写入自定义日志文件而不是Apache的error_log中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有部署在生产与Apache和mod_wsgi的Python应用程序。
在我的应用程序,我已经安装日志如下:

I have a Python application which is deployed in production with Apache and mod_wsgi. In my application, I have setup logging as follows:

配置文件:

log_file = os.path.dirname(os.path.abspath(__file__)) + "/app.log"
log_level = logging.DEBUG

__ __初始化PY 文件:

root_logger = logging.getLogger()
root_logger.setLevel(config.log_level)

log_format = logging.Formatter("%(asctime)s [%(levelname)s] [%(name)s] %(message)s")

file_handler = logging.FileHandler(config.log_file)
file_handler.setFormatter(log_format)

stream_handler = logging.StreamHandler()
stream_handler.setFormatter(log_format)

root_logger.addHandler(file_handler)
root_logger.addHandler(stream_handler)

在创建瓶的应用程序之前,此code被执行。

This code is executed before the Flask application is created.

所有的记录,是不是一个错误/异常正确地记录到该自定义文件。

All logging that is not an error/exception is correctly logged to this custom file.

但是,如果Web服务器正在运行时,会产生异常,错误,而不是写入到Apache的错误日志,而不是一个在这里配置。如何配置Python的日志/ mod_wsgi的/阿帕奇让一切都写在这里配置的日志文件?

However, if an exception is generated while the web server is running, the errors are instead written to Apache's error log instead of the one configured here. How do I configure Python's logging/mod_wsgi/Apache so that everything is written to the log file configured here?

推荐答案

添加您的记录器应用WSGI记录器,从的瓶中的错误处理文档

Add your logger to WSGI application loggers, from flask error handling docs:

if not app.debug:
    import logging
    from themodule import TheHandlerYouWant
    file_handler = TheHandlerYouWant(...)
    file_handler.setLevel(logging.WARNING)
    app.logger.addHandler(file_handler)

这篇关于Python的mod_wsgi的写入自定义日志文件而不是Apache的error_log中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆