about summary refs log tree commit diff
path: root/gn_auth
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2026-05-01 14:49:39 -0500
committerFrederick Muriuki Muriithi2026-05-01 14:49:39 -0500
commitc8176603fb3d6ac8cd4c917dde397dd0de9faf03 (patch)
tree7733bf0d7e344742701f0d0525e0824f5dcff72e /gn_auth
parent3443869f9a6435ee37f20c983a32e3e94d7290cd (diff)
downloadgn-auth-c8176603fb3d6ac8cd4c917dde397dd0de9faf03.tar.gz
Enable turning logging on/off by module.
To help with debugging and traceability, both in development and
production, we need to be able to turn individual module loggers on or
off in a flexible way. This commit enables that.
Diffstat (limited to 'gn_auth')
-rw-r--r--gn_auth/__init__.py36
-rw-r--r--gn_auth/settings.py2
2 files changed, 14 insertions, 24 deletions
diff --git a/gn_auth/__init__.py b/gn_auth/__init__.py
index d6591e5..f015e5e 100644
--- a/gn_auth/__init__.py
+++ b/gn_auth/__init__.py
@@ -61,33 +61,24 @@ def load_secrets_conf(app: Flask) -> None:
         app.config.from_pyfile(secretsfile)
 
 
-def dev_loggers(appl: Flask) -> None:
+def dev_loggers(appl: Flask) -> logging.Logger:
     """Setup the logging handlers."""
     stderr_handler = logging.StreamHandler(stream=sys.stderr)
     appl.logger.addHandler(stderr_handler)
+    appl.logger.setLevel(appl.config["LOGLEVEL"])
 
-    root_logger = logging.getLogger()
-    root_logger.addHandler(stderr_handler)
-    root_logger.setLevel(appl.config["LOGLEVEL"])
+    return appl.logger
 
 
-def gunicorn_loggers(appl: Flask) -> None:
+def gunicorn_loggers(appl: Flask) -> logging.Logger:
     """Use gunicorn logging handlers for the application."""
     logger = logging.getLogger("gunicorn.error")
     appl.logger.handlers = logger.handlers
     appl.logger.setLevel(logger.level)
+    return appl.logger
 
 
-_LOGGABLE_MODULES_ = (
-    "gn_auth.errors",
-    "gn_auth.errors.common",
-    "gn_auth.errors.authlib",
-    "gn_auth.errors.http.http_4xx_errors",
-    "gn_auth.errors.http.http_5xx_errors"
-)
-
-
-def setup_logging(appl: Flask) -> None:
+def setup_logging(appl: Flask, loggable_modules: tuple[str, ...] = tuple()) -> None:
     """
     Setup the loggers according to the WSGI server used to run the application.
     """
@@ -96,14 +87,11 @@ def setup_logging(appl: Flask) -> None:
     # https://peps.python.org/pep-3333/#id4
     software, *_version_and_comments = os.environ.get(
         "SERVER_SOFTWARE", "").split('/')
-    if bool(software):
-        gunicorn_loggers(appl)
-    else:
-        dev_loggers(appl)
-
-    loglevel = logging.getLevelName(appl.logger.getEffectiveLevel())
-    for module_logger in _LOGGABLE_MODULES_:
-        logging.getLogger(module_logger).setLevel(loglevel)
+    logger = gunicorn_loggers(appl) if bool(software) else dev_loggers(appl)
+    for _logger in (
+            item for item in logger.manager.loggerDict.values()
+            if isinstance(item, logging.Logger)):
+        _logger.addFilter(lambda record: record.name in loggable_modules)
 
 
 def create_app(config: Optional[dict] = None) -> Flask:
@@ -123,7 +111,7 @@ def create_app(config: Optional[dict] = None) -> Flask:
     load_secrets_conf(app)
     # ====== END: Setup configuration ======
 
-    setup_logging(app)
+    setup_logging(app, tuple(app.config.get("LOGGABLE_MODULES", [])))
     check_mandatory_settings(app)
 
     setup_oauth2_server(app)
diff --git a/gn_auth/settings.py b/gn_auth/settings.py
index d59e997..fe0ac92 100644
--- a/gn_auth/settings.py
+++ b/gn_auth/settings.py
@@ -49,3 +49,5 @@ EMAIL_ADDRESS = "no-reply@uthsc.edu"
 
 ## Variable settings for various emails going out to users
 AUTH_EMAILS_EXPIRY_MINUTES = 15
+
+LOGGABLE_MODULES = ["gn_auth"]