【问题标题】:logging doesn't work using wsgi, no file is created使用 wsgi 日志记录不起作用,没有创建文件
【发布时间】:2017-08-14 10:57:07
【问题描述】:

我正在运行这个烧瓶应用程序来测试日志记录。它在我的本地系统上运行良好,但如果我使用 mod_wsgi 在 apache 服务器上运行它,它就不起作用。没有错误,但是没有创建 foo.log。

我正在使用 python 3.5

代码如下所示:

from flask import Flask
import logging
from logging.handlers import RotatingFileHandler

app = Flask(__name__)

@app.route('/')
def foo():
    app.logger.warning('A warning occurred (%d apples)', 42)
    app.logger.error('An error occurred')
    app.logger.info('Info')
    return "foo"

if __name__ == '__main__':
    handler = RotatingFileHandler('foo.log', maxBytes=10000, backupCount=1)
    handler.setLevel(logging.INFO)
    app.logger.addHandler(handler)
    app.run(host='0.0.0.0')

wsgi 文件如下所示:

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'Add your secret key'

【问题讨论】:

    标签: python logging mod-wsgi wsgi


    【解决方案1】:

    在使用 Apache/mod_wsgi 时,不要为 Python logging 模块使用单独的日志文件。而是将logging 模块配置为将消息输出到终端的流处理程序(stdout/stderr)。然后这些消息将被收集到 Apache 错误日志中。然后使用 Apache 或系统机制来轮换日志文件。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。我使用 Apache 的 mod_wsgi 来运行 Django 应用程序。

      最初的日志文件是'/tmp/debug.log',wsgi的日志文件没有错误但是没有创建文件“/tmp/debug.log”。

      然后,我将文件更改为“/var/log/httpd/debug.log”,我发现如下错误:

      ValueError: Unable to configure handler 'file': [Errno 13] Permission denied: '/var/log/httpd/debug.log'
      

      最后,我新建一个目录,并将所有者更改为 apache,如下所示:

      make /var/log/test
      chown apache:apache /var/log/test
      

      现在,创建了日志文件“/var/log/test/debug.log”。

      【讨论】:

        【解决方案3】:

        通过 apache 服务器运行时,脚本的密码不一定与终端密码相同。

        要么:

        1. 使用完整路径而不是foo.log

        或者:

        1. 在您的系统中搜索 foo.log 并找出 apache 决定将其放置在哪里 :)

        【讨论】:

        • 您不会找到它,因为 Apache 用户在运行时无法写入 Apache 的当前工作目录。
        猜你喜欢
        • 1970-01-01
        • 2021-11-19
        • 2011-02-12
        • 2010-11-20
        • 2018-08-24
        • 2011-12-27
        • 2013-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多