From 5fee57f8c27d7212afe8a889ea94594729d5f065 Mon Sep 17 00:00:00 2001 From: ougongchang Date: Tue, 1 Dec 2020 16:19:28 +0800 Subject: [PATCH] Redirects stdout in the Gunicorn process to a log file Gunicorn is currently running using a subprocess. When the stdout log information exists in the process, it is printed to the screen. This will greatly affect the user experience. So we need to redirect the stdout information to a log file to avoid printing logs in the console. --- mindinsight/backend/run.py | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/mindinsight/backend/run.py b/mindinsight/backend/run.py index 80e4d2ce..3529301e 100644 --- a/mindinsight/backend/run.py +++ b/mindinsight/backend/run.py @@ -228,18 +228,8 @@ class GunicornLogger(Logger): """Get log format.""" return time.strftime('[%Y-%m-%d-%H:%M:%S %z]') - def rewrite_stdout(self, message): - if message.strip(): - self.error_log.info(message) - - def rewrite_stderr(self, message): - if message.strip(): - self.error_log.error(message) - def setup(self, cfg): """Rewrite the setup method of Logger, and we don't need to do anything""" - sys.stdout.write = self.rewrite_stdout - sys.stderr.write = self.rewrite_stderr def _get_all_ip_addresses(host): @@ -263,8 +253,6 @@ def _get_all_ip_addresses(host): def start(): """Start web service.""" - errorlog_abspath = _get_error_log_path() - gunicorn_conf_file = os.path.join(WEB_CONFIG_DIR, "gunicorn_conf.py") cmd = "gunicorn " \ "-b {host}:{port} {app_module} " \ @@ -279,9 +267,12 @@ def start(): log_format=settings.GUNICORN_ACCESS_FORMAT ) - log_size = _get_file_size(errorlog_abspath) + error_log_abspath = _get_error_log_path() + log_size = _get_file_size(error_log_abspath) - console = setup_logger('mindinsight', 'console', console=True, logfile=False, formatter='%(message)s') + # Init the logger file + setup_logger('gunicorn', 'error') + log_handler = open(error_log_abspath, 'a+') # start server process = subprocess.Popen( @@ -289,7 +280,7 @@ def start(): shell=False, # Change stdout to DEVNULL to prevent broken pipe error when creating new processes. stdin=subprocess.DEVNULL, - stdout=None, + stdout=log_handler, stderr=subprocess.STDOUT ) @@ -297,11 +288,12 @@ def start(): time.sleep(1) # check if gunicorn application is running + console = setup_logger('mindinsight', 'console', console=True, logfile=False, formatter='%(message)s') if process.poll() is not None: - console.error("Start MindInsight failed. See log for details.") + console.error("Start MindInsight failed. See log for details, log path: %s.", error_log_abspath) sys.exit(1) else: - state_result = _check_server_start_stat(errorlog_abspath, log_size) + state_result = _check_server_start_stat(error_log_abspath, log_size) # print gunicorn start state to stdout label = 'Web address:' for ip in _get_all_ip_addresses(settings.HOST):