Disable console messages in Flask server

前端 未结 10 831
旧巷少年郎
旧巷少年郎 2020-11-28 07:42

I have a Flask server running in standalone mode (using app.run()). But, I don\'t want any messages in the console, like

127.0.0.1 - - [15/Feb/2         


        
10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 08:36

    You can set level of the Werkzeug logger to ERROR, in that case only errors are logged:

    import logging
    log = logging.getLogger('werkzeug')
    log.setLevel(logging.ERROR)
    

    Here is a fully working example tested on OSX, Python 2.7.5, Flask 0.10.0:

    from flask import Flask
    app = Flask(__name__)
    
    import logging
    log = logging.getLogger('werkzeug')
    log.setLevel(logging.ERROR)
    
    @app.route("/")
    def hello():
        return "Hello World!"
    
    if __name__ == "__main__":
        app.run()
    

提交回复
热议问题