无法使用gunicorn守护程序模式为Flask进行日志记录工作

Yul*_*ong 8 python logging flask gunicorn

我正在使用gunicorn + Nginx运行Flask Web应用程序.我在daemon模式中运行gunicorn .我配置了gunicorn和nginx来记录他们对文件的访问和错误.但我无法将Flask日志记录到文件中.

我使用shell文件用gunicorn启动我的应用程序:

   #!/bin/bash

   export VIRTUAL_ENV="/to/virtual/path"
   export PATH="$VIRTUAL_ENV/bin:$PATH"
   source "$VIRTUAL_ENV/bin/activate"

   NAME="hello"
   NUM_WORKERS=1

   exec gunicorn hello:app \
       --name $NAME \
       --workers $NUM_WORKERS \
       --log-level=debug \
       --daemon \
       --pid $VIRTUAL_ENV/logs/pid_gunicorn \
       --access-logfile $VIRTUAL_ENV/logs/access_gunicorn.log \
       --error-logfile $VIRTUAL_ENV/logs/error_gunicorn.log    
Run Code Online (Sandbox Code Playgroud)

在我的烧瓶应用程序中,我根据文档要求添加日志记录:

app.debug = False
...
if __name__ == '__main__':
    if app.debug != True:
        import logging
        from logging.handlers import RotatingFileHandler
        handler = RotatingFileHandler("flask.log", maxBytes=10000, backupCount=1)
        handler.setLevel(logging.DEBUG)
        app.logger.addHandler(handler)
        app.logger.debug("test!!")
    app.run()
Run Code Online (Sandbox Code Playgroud)

我还在app.logger.debug其他地方添加了.

当我gunicorn没有开始时--daemon,日志文件工作正常.但是一旦我添加,--daemon就不会生成任何日志.

我尝试使用,print但它只能没有--daemon.

我已经搜索了一段时间,似乎 gunicorn不支持应用程序日志记录.但我认为记录到文件会没问题?

有人知道如何在我的设置下注销文件吗?

CES*_*SCO 0

运行守护进程的 Python 方式是使用类似Supervisord 的东西,忘记 bash,它是 Python。您是否考虑过使用 nginx 作为代理通行证?Gunicorn 可以处理 WSGI。我认为它从 1.3.13 开始就可用。它适用于 websockets,但即使您运行 http 协议也能工作。

就像是

server {
    listen 80;
    server_name localhost;
    access_log /var/log/nginx/example.log;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_redirect off;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
Run Code Online (Sandbox Code Playgroud)