Why is Flask application not creating any logs when hosted by Gunicorn?

后端 未结 3 733
独厮守ぢ
独厮守ぢ 2020-12-24 02:00

I\'m trying to add logging to a web application which uses Flask.

When hosted using the built-in server (i.e. python3 server.py), logging works. When ho

3条回答
  •  梦毁少年i
    2020-12-24 02:41

    This approach works for me: Import the Python logging module and add gunicorn's error handlers to it. Then your logger will log into the gunicorn error log file:

    import logging
    
    app = Flask(__name__)
    
    gunicorn_error_logger = logging.getLogger('gunicorn.error')
    app.logger.handlers.extend(gunicorn_error_logger.handlers)
    app.logger.setLevel(logging.DEBUG)
    app.logger.debug('this will show in the log')
    

    My Gunicorn startup script is configured to output log entries to a file like so:

    gunicorn main:app \
        --workers 4 \
        --bind 0.0.0.0:9000 \
        --log-file /app/logs/gunicorn.log \
        --log-level DEBUG \
        --reload
    

提交回复
热议问题