From f4fd4ceb2261461a77dad0bdb4c4d4eebc95cd3e Mon Sep 17 00:00:00 2001 From: John M <5000729+jmuchovej@users.noreply.github.com> Date: Wed, 24 Mar 2021 18:44:17 -0400 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9C=A8=20Attempting=20to=20enable=20stre?= =?UTF-8?q?ama=20handling=20for=20Heroku?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- psiturk/experiment.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/psiturk/experiment.py b/psiturk/experiment.py index afb8bf30c..dfc31ddc0 100644 --- a/psiturk/experiment.py +++ b/psiturk/experiment.py @@ -35,15 +35,15 @@ # Setup logging if 'ON_CLOUD' in os.environ: - LOG_FILE_PATH = None + handler = logging.StreamHandler(sys.stderr) else: - LOG_FILE_PATH = os.path.join(os.getcwd(), CONFIG.get("Server Parameters", - "logfile")) + file_path = os.path.join(os.getcwd(), CONFIG.get("Server Parameters", "logfile")) + handler = logging.FileHandler(filename=file_path) LOG_LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL] LOG_LEVEL = LOG_LEVELS[CONFIG.getint('Server Parameters', 'loglevel')] -logging.basicConfig(filename=LOG_FILE_PATH, format='%(asctime)s %(message)s', +logging.basicConfig(handler=handler, format='%(asctime)s %(message)s', level=LOG_LEVEL) From cdb19d2bc445476ddc916be5b66642ae9ee9000a Mon Sep 17 00:00:00 2001 From: John M <5000729+jmuchovej@users.noreply.github.com> Date: Wed, 24 Mar 2021 18:47:45 -0400 Subject: [PATCH 2/5] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Typo=20in=20basicConfi?= =?UTF-8?q?g=20handlers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- psiturk/experiment.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/psiturk/experiment.py b/psiturk/experiment.py index dfc31ddc0..a7c2f5407 100644 --- a/psiturk/experiment.py +++ b/psiturk/experiment.py @@ -34,16 +34,20 @@ CONFIG.load_config() # Setup logging +# taken from: https://stackoverflow.com/a/44760039/2714651 +handlers = [] if 'ON_CLOUD' in os.environ: handler = logging.StreamHandler(sys.stderr) + handlers += [stream_handler] else: file_path = os.path.join(os.getcwd(), CONFIG.get("Server Parameters", "logfile")) - handler = logging.FileHandler(filename=file_path) + file_handler = logging.FileHandler(filename=file_path) + handlers += [file_handler] LOG_LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL] LOG_LEVEL = LOG_LEVELS[CONFIG.getint('Server Parameters', 'loglevel')] -logging.basicConfig(handler=handler, format='%(asctime)s %(message)s', +logging.basicConfig(handlers=handlers, format='%(asctime)s %(message)s', level=LOG_LEVEL) From 8d45b182f18db0eb44eb30a0df87a68d1638b2b0 Mon Sep 17 00:00:00 2001 From: John M <5000729+jmuchovej@users.noreply.github.com> Date: Wed, 24 Mar 2021 18:57:45 -0400 Subject: [PATCH 3/5] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Fix=20stream=5Fhandler?= =?UTF-8?q?=20typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- psiturk/experiment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/psiturk/experiment.py b/psiturk/experiment.py index a7c2f5407..e3a8777c4 100644 --- a/psiturk/experiment.py +++ b/psiturk/experiment.py @@ -37,7 +37,7 @@ # taken from: https://stackoverflow.com/a/44760039/2714651 handlers = [] if 'ON_CLOUD' in os.environ: - handler = logging.StreamHandler(sys.stderr) + stream_handler = logging.StreamHandler(sys.stderr) handlers += [stream_handler] else: file_path = os.path.join(os.getcwd(), CONFIG.get("Server Parameters", "logfile")) From 79f16bdf1f2464d064d61fc7a284b4848a843809 Mon Sep 17 00:00:00 2001 From: Dave Eargle Date: Sat, 27 Mar 2021 15:11:56 -0600 Subject: [PATCH 4/5] defer all logging to gunicorn if gunicorn is in use --- psiturk/experiment.py | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/psiturk/experiment.py b/psiturk/experiment.py index e3a8777c4..52b6de93c 100644 --- a/psiturk/experiment.py +++ b/psiturk/experiment.py @@ -33,26 +33,6 @@ CONFIG = PsiturkConfig() CONFIG.load_config() -# Setup logging -# taken from: https://stackoverflow.com/a/44760039/2714651 -handlers = [] -if 'ON_CLOUD' in os.environ: - stream_handler = logging.StreamHandler(sys.stderr) - handlers += [stream_handler] -else: - file_path = os.path.join(os.getcwd(), CONFIG.get("Server Parameters", "logfile")) - file_handler = logging.FileHandler(filename=file_path) - handlers += [file_handler] - -LOG_LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, - logging.CRITICAL] -LOG_LEVEL = LOG_LEVELS[CONFIG.getint('Server Parameters', 'loglevel')] -logging.basicConfig(handlers=handlers, format='%(asctime)s %(message)s', - level=LOG_LEVEL) - - -# Status codes - # Let's start # =========== @@ -60,8 +40,23 @@ # experiment server logging if 'gunicorn' in os.environ.get('SERVER_SOFTWARE', ''): - gunicorn_error_logger = logging.getLogger('gunicorn.error') - app.logger.handlers.extend(gunicorn_error_logger.handlers) + gunicorn_logger = logging.getLogger('gunicorn.error') + app.logger.handlers = gunicorn_logger.handlers + app.logger.setLevel(gunicorn_logger.level) +else: + errorlog = CONFIG.get("Server Parameters", "errorlog") + if errorlog == '-': + handler = logging.StreamHandler(sys.stderr) + else: + file_path = os.path.join(os.getcwd(), errorlog) + handler = logging.FileHandler(filename=file_path) + + LOG_LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, + logging.CRITICAL] + LOG_LEVEL = LOG_LEVELS[CONFIG.getint('Server Parameters', 'loglevel')] + + logging.basicConfig(handlers=[handler], format='%(asctime)s %(message)s', + level=LOG_LEVEL) # Set cache timeout to 10 seconds for static files app.config.update(SEND_FILE_MAX_AGE_DEFAULT=10) From 990db90dc34a36012c1edf88b3172c62b145179c Mon Sep 17 00:00:00 2001 From: Dave Eargle Date: Sat, 27 Mar 2021 19:34:18 -0600 Subject: [PATCH 5/5] undo defer to gunicorn only set filename logger if not logging to `-` (flask defaults to logging to stdout) --- psiturk/experiment.py | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/psiturk/experiment.py b/psiturk/experiment.py index 52b6de93c..0aca8121f 100644 --- a/psiturk/experiment.py +++ b/psiturk/experiment.py @@ -33,30 +33,21 @@ CONFIG = PsiturkConfig() CONFIG.load_config() +LOG_LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, +logging.CRITICAL] +LOG_LEVEL = LOG_LEVELS[CONFIG.getint('Server Parameters', 'loglevel')] + +logfile = CONFIG.get("Server Parameters", "errorlog") +if logfile != '-': + file_path = os.path.join(os.getcwd(), logfile) + logging.basicConfig(filename=file_path, format='%(asctime)s %(message)s', + level=LOG_LEVEL) + # Let's start # =========== app = Flask("Experiment_Server") - -# experiment server logging -if 'gunicorn' in os.environ.get('SERVER_SOFTWARE', ''): - gunicorn_logger = logging.getLogger('gunicorn.error') - app.logger.handlers = gunicorn_logger.handlers - app.logger.setLevel(gunicorn_logger.level) -else: - errorlog = CONFIG.get("Server Parameters", "errorlog") - if errorlog == '-': - handler = logging.StreamHandler(sys.stderr) - else: - file_path = os.path.join(os.getcwd(), errorlog) - handler = logging.FileHandler(filename=file_path) - - LOG_LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, - logging.CRITICAL] - LOG_LEVEL = LOG_LEVELS[CONFIG.getint('Server Parameters', 'loglevel')] - - logging.basicConfig(handlers=[handler], format='%(asctime)s %(message)s', - level=LOG_LEVEL) +app.logger.setLevel(LOG_LEVEL) # Set cache timeout to 10 seconds for static files app.config.update(SEND_FILE_MAX_AGE_DEFAULT=10)