【问题标题】:Python mod_wsgi write to custom log file instead of apache error_logPython mod_wsgi 写入自定义日志文件而不是 apache error_log
【发布时间】:2014-09-24 13:47:46
【问题描述】:

我有一个 Python 应用程序,它使用 Apache 和 mod_wsgi 部署在生产环境中。 在我的应用程序中,我设置了如下日志记录:

config文件:

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

__init__.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)

此代码在创建 Flask 应用程序之前执行。

所有不是错误/异常的日志记录都正确记录到此自定义文件中。

但是,如果在 Web 服务器运行时产生异常,则错误会被写入 Apache 的错误日志,而不是此处配置的日志。如何配置 Python 的 logging/mod_wsgi/Apache 以便将所有内容写入此处配置的日志文件?

【问题讨论】:

    标签: python apache flask mod-wsgi


    【解决方案1】:

    将您的记录器添加到 WSGI 应用程序记录器,来自 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)
    

    【讨论】:

    • 补充一点,我在上面设置的日志记录配置也可以将错误记录到正确的文件中。我发现解决方案是删除以下内容:app.debug = True,然后所有指定的日志记录都按预期工作。
    • 很高兴知道您已经解决了问题。
    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    相关资源
    最近更新 更多