How do I write Flask's excellent debug log message to a file in production?

前端 未结 5 1882
囚心锁ツ
囚心锁ツ 2020-12-22 17:29

I have a Flask application that works well and produces an occasional error, which is visible when it is running with debug=True:

if __name__ =         


        
5条回答
  •  时光说笑
    2020-12-22 18:02

    If you are using gunicorn to run your Flask app, you can log all Flask exceptions to the gunicorn logs by adding the gunicorn error handlers to the Flask logger:

    In module/__init__.py:

    @app.before_first_request
    def setup_logging():
        if not app.debug:
            import logging
            gunicorn_logger = logging.getLogger('gunicorn.error')
            for handler in gunicorn_logger.handlers:
                app.logger.addHandler(handler)
    

提交回复
热议问题