From e0efe4a12a5819b51879e643d0adfce1ed440b84 Mon Sep 17 00:00:00 2001 From: Harry White Date: Wed, 23 Oct 2019 20:31:12 +1100 Subject: [PATCH] Disable all logging and redirect stdout when quiet Redirecting stdout is required to squash all messages as flask.cli.show_server_banner uses printing instead of logging --- grip/app.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/grip/app.py b/grip/app.py index 123d00f..24e3d15 100644 --- a/grip/app.py +++ b/grip/app.py @@ -11,6 +11,7 @@ import threading import time import errno +import contextlib from traceback import format_exc try: from urlparse import urlparse @@ -109,8 +110,10 @@ def __init__(self, source=None, auth=None, renderer=None, self.quiet = quiet if self.quiet: import logging + # Disable werkzeug and flask logging + self.logger.setLevel(logging.CRITICAL) log = logging.getLogger('werkzeug') - log.setLevel(logging.ERROR) + log.setLevel(logging.CRITICAL) # Overridable attributes if self.renderer is None: @@ -434,10 +437,19 @@ def run(self, host=None, port=None, debug=None, use_reloader=None, start_browser_when_ready(host, port, self._shutdown_event) if open_browser else None) - # Run local server - super(Grip, self).run(host, port, debug=debug, - use_reloader=use_reloader, - threaded=True) + # Redirect stdout if requred + output_file = sys.stdout + if self.quiet: + output_file = open(os.devnull, 'w') + + with contextlib.redirect_stdout(output_file): + # Run local server + super(Grip, self).run(host, port, debug=debug, + use_reloader=use_reloader, + threaded=True) + + if output_file != sys.stdout: + output_file.close() # Signal to the polling and browser threads that they should exit if not self.quiet: