Browse Source

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.
tags/v1.1.0
ougongchang 5 years ago
parent
commit
5fee57f8c2
1 changed files with 9 additions and 17 deletions
  1. +9
    -17
      mindinsight/backend/run.py

+ 9
- 17
mindinsight/backend/run.py View File

@@ -228,18 +228,8 @@ class GunicornLogger(Logger):
"""Get log format.""" """Get log format."""
return time.strftime('[%Y-%m-%d-%H:%M:%S %z]') 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): def setup(self, cfg):
"""Rewrite the setup method of Logger, and we don't need to do anything""" """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): def _get_all_ip_addresses(host):
@@ -263,8 +253,6 @@ def _get_all_ip_addresses(host):


def start(): def start():
"""Start web service.""" """Start web service."""
errorlog_abspath = _get_error_log_path()

gunicorn_conf_file = os.path.join(WEB_CONFIG_DIR, "gunicorn_conf.py") gunicorn_conf_file = os.path.join(WEB_CONFIG_DIR, "gunicorn_conf.py")
cmd = "gunicorn " \ cmd = "gunicorn " \
"-b {host}:{port} {app_module} " \ "-b {host}:{port} {app_module} " \
@@ -279,9 +267,12 @@ def start():
log_format=settings.GUNICORN_ACCESS_FORMAT 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 # start server
process = subprocess.Popen( process = subprocess.Popen(
@@ -289,7 +280,7 @@ def start():
shell=False, shell=False,
# Change stdout to DEVNULL to prevent broken pipe error when creating new processes. # Change stdout to DEVNULL to prevent broken pipe error when creating new processes.
stdin=subprocess.DEVNULL, stdin=subprocess.DEVNULL,
stdout=None,
stdout=log_handler,
stderr=subprocess.STDOUT stderr=subprocess.STDOUT
) )


@@ -297,11 +288,12 @@ def start():
time.sleep(1) time.sleep(1)


# check if gunicorn application is running # check if gunicorn application is running
console = setup_logger('mindinsight', 'console', console=True, logfile=False, formatter='%(message)s')
if process.poll() is not None: 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) sys.exit(1)
else: 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 # print gunicorn start state to stdout
label = 'Web address:' label = 'Web address:'
for ip in _get_all_ip_addresses(settings.HOST): for ip in _get_all_ip_addresses(settings.HOST):


Loading…
Cancel
Save