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

后端 未结 3 737
独厮守ぢ
独厮守ぢ 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条回答
  •  再見小時候
    2020-12-24 02:33

    When you use python3 server.py you are running the server3.py script.

    When you use gunicorn server:flaskApp ... you are running the gunicorn startup script which then imports the module server and looks for the variable flaskApp in that module.

    Since server.py is being imported the __name__ var will contain "server", not "__main__" and therefore you log handler setup code is not being run.

    You could simply move the log handler setup code outside of the if __name__ == "__main__": stanza. But ensure that you keep flaskApp.run() in there since you do not want that to be run when gunicorn imports server.

    More about what does if __name__ == “__main__”: do?

提交回复
热议问题