Disable console messages in Flask server

前端 未结 10 787
旧巷少年郎
旧巷少年郎 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:30

    A brute force way to do it if you really don't want anything to log into the console beside print() statements is to logging.basicConfig(level=logging.FATAL). This would disable all logs that are of status under fatal. It would not disable printing but yeah, just a thought :/

    EDIT: I realized it would be selfish of me not to put a link to the documentation I used :) https://docs.python.org/3/howto/logging.html#logging-basic-tutorial

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-11-28 08:36

    Another reason you may want to change the logging output is for tests, and redirect the server logs to a log file.

    I couldn't get the suggestion above to work either, it looks like loggers are setup as part of the app starting. I was able to get it working by changing the log levels after starting the app:

    ... (in setUpClass)
    server = Thread(target=lambda: app.run(host=hostname, port=port, threaded=True))
    server.daemon = True
    server.start()
    wait_for_boot(hostname, port)  # curls a health check endpoint
    
    log_names = ['werkzeug']
    app_logs = map(lambda logname: logging.getLogger(logname), log_names)
    file_handler = logging.FileHandler('log/app.test.log', 'w')
    
    for app_log in app_logs:
        for hdlr in app_log.handlers[:]:  # remove all old handlers
            app_log.removeHandler(hdlr)
    
        app_log.addHandler(file_handler)
    

    Unfortunately the * Running on localhost:9151 and the first health check is still printed to standard out, but when running lots of tests it cleans up the output a ton.

    "So why log_names?", you ask. In my case there were some extra logs I needed to get rid of. I was able to find which loggers to add to log_names via:

    from flask import Flask
    app = Flask(__name__)
    
    import logging
    print(logging.Logger.manager.loggerDict)
    

    Side note: It would be nice if there was a flaskapp.getLogger() or something so this was more robust across versions. Any ideas?

    Some more key words: flask test log remove stdout output

    thanks to:

    • http://code.activestate.com/lists/python-list/621740/ and
    • How to change filehandle with Python logging on the fly with different classes and imports
    0 讨论(0)
  • 2020-11-28 08:37

    The first point: In according to official Flask documentation, you shouldn't run Flask application using app.run(). The best solution is using uwsgi, so you can disable default flask logs using command "--disable-logging"

    For example:

    uwsgi --socket 0.0.0.0:8001 --disable-logging --protocol=http -w app:app

    0 讨论(0)
提交回复
热议问题