Disable console messages in Flask server

I have a Flask server working offline (using app.run() ). But I do not want any messages in the console, for example

 127.0.0.1 - - [15/Feb/2013 10:52:22] "GET /index.html HTTP/1.1" 200 - ... 

How to disable verbose mode?

+69
python flask
Feb 15 '13 at 5:29
source share
10 answers

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

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

Here is a complete working test case example for 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() 
+105
Aug 22 '13 at 11:57
source share

This solution provides you with a way to get your own printouts and stack traces, but without information level logs they suck from the bulb like 127.0.0.1 - - [15/Feb/2013 10:52:22] "GET/index.html HTTP/1.1" 200

 from flask import Flask import logging app = Flask(__name__) log = logging.getLogger('werkzeug') log.disabled = True 
+9
Feb 15 '19 at 12:48
source share

@Drewes solution works most of the time, but in some cases I still tend to get werkzeug logs. If you really do not want to see any of them, I suggest you turn it off this way.

 from flask import Flask import logging app = Flask(__name__) log = logging.getLogger('werkzeug') log.disabled = True app.logger.disabled = True 

For me, this failed when there was abort(500) .

+7
Apr 10 '18 at 10:25
source share

Another reason you can change the log output is to test and redirect server logs to a log file.

I could not get the above sentence to work, it seems that the loggers are configured as part of the application launch. I managed to get it working by changing the log levels after starting the application:

 ... (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, * Running on localhost:9151 and the first health check are still printed at the standard level, but when a large number of tests are run, it clears the output per ton.

“So why log_names ?” You ask. In my case, there were some additional magazines that I needed to get rid of. I was able to find which loggers to add to log_names using:

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

Side note: It would be nice if there was flaskapp.getLogger () or something else, so it was more reliable for versions. Any ideas?

A few more keywords: check box log deletes stdout output

thanks to:

+6
Oct 12 '16 at 2:29
source share

If you are using a WSGI server, set the log to None.

 gevent_server = gevent.pywsgi.WSGIServer(("0.0.0.0", 8080), app,log = None) 
+5
Oct 29 '18 at 6:46
source share

To suppress Serving Flask app... :

 os.environ['WERKZEUG_RUN_MAIN'] = 'true' app.run() 
+3
May 12 '19 at 21:19
source share

Late answer, but I found a way to suppress EVERY AND EVERY CONSOLE MESSAGE (including those that were displayed during abort(...) ).

 import os import logging logging.getLogger('werkzeug').disabled = True os.environ['WERKZEUG_RUN_MAIN'] = 'true' 

This is basically a combination of the answers of Glory V and Tom Wojcik

+2
Jul 02 '19 at 16:40
source share

None of the other answers worked correctly for me, but I found a solution based on Peter's comment . Flask apparently no longer uses logging for logging and switched to the click package. Overriding click.echo and click.secho I removed the Flask startup message from app.run() .

 import logging import click from flask import Flask app = Flask(__name__) log = logging.getLogger('werkzeug') log.setLevel(logging.ERROR) def secho(text, file=None, nl=None, err=None, color=None, **styles): pass def echo(text, file=None, nl=None, err=None, color=None, **styles): pass click.echo = echo click.secho = secho @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run() 

Between setting the logging level to ERROR and overriding click methods with empty functions, you should avoid all log output without errors.

+1
Jul 18 '19 at 3:23
source share

A crude way to do this if you really do not want anything logging.basicConfig(level=logging.FATAL) to the console other than print () logging.basicConfig(level=logging.FATAL) is logging.basicConfig(level=logging.FATAL) . This would disable all logs that have status under fatal. This will not disable printing, but yes, just a thought: /

EDIT: I realized that 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
Feb 23 '19 at 4:00
source share

First point: according to the official Flask documentation, you should not run the Flask application using app.run (). The best solution is to use uwsgi, so you can disable the default vial logs with the command --disable-logging

For example:

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

0
Aug 26 '19 at 12:22
source share



All Articles