From 95f067a542424b76022595a74d660a7e84158f38 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 21 Apr 2023 05:22:05 +0300 Subject: Setup module for logging During development, we need logging sometimes to help with troubleshooting problems. This commit provides a module to help set up the logging in a separate module from the app module. --- gn3/app.py | 4 ++++ gn3/loggers.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 gn3/loggers.py (limited to 'gn3') diff --git a/gn3/app.py b/gn3/app.py index 0c2f9f9..b4b4b43 100644 --- a/gn3/app.py +++ b/gn3/app.py @@ -7,6 +7,7 @@ from typing import Union from flask import Flask from flask_cors import CORS # type: ignore +from gn3.loggers import setup_app_handlers from gn3.api.gemma import gemma from gn3.api.rqtl import rqtl from gn3.api.general import general @@ -42,6 +43,9 @@ def create_app(config: Union[Dict, str, None] = None) -> Flask: elif config.endswith(".py"): app.config.from_pyfile(config) + # DO NOT log anything before this point + setup_app_handlers(app) + CORS( app, origins=app.config["CORS_ORIGINS"], diff --git a/gn3/loggers.py b/gn3/loggers.py new file mode 100644 index 0000000..a74ae7e --- /dev/null +++ b/gn3/loggers.py @@ -0,0 +1,21 @@ +"""Setup loggers""" +import sys +import logging +from logging import StreamHandler + +# ========== Setup formatters ========== +# ========== END: Setup formatters ========== + +def loglevel(app): + """'Compute' the LOGLEVEL from the application.""" + return logging.DEBUG if app.config.get("DEBUG", False) else logging.WARNING + +def setup_app_handlers(app): + """Setup the logging handlers for the application `app`.""" + # ========== Setup handlers ========== + stderr_handler = StreamHandler(stream=sys.stderr) + stderr_handler.setLevel(loglevel(app)) + app.logger.addHandler(stderr_handler) + # ========== END: Setup handlers ========== + root_logger = logging.getLogger() + root_logger.addHandler(stderr_handler) -- cgit v1.2.3