about summary refs log tree commit diff
path: root/gn_libs/logging.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2026-01-05 12:43:59 -0600
committerFrederick Muriuki Muriithi2026-01-05 12:43:59 -0600
commit06e90d15fe285ab37319c11f3c58be25e8bde339 (patch)
tree77b8c951daf8525c4696efd04f07897b861e5166 /gn_libs/logging.py
parentedfbd94378980e1189fcfb769a207e0519af228b (diff)
downloadgn-libs-06e90d15fe285ab37319c11f3c58be25e8bde339.tar.gz
Add basic logging initialisation functions.
Diffstat (limited to 'gn_libs/logging.py')
-rw-r--r--gn_libs/logging.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/gn_libs/logging.py b/gn_libs/logging.py
new file mode 100644
index 0000000..0317be6
--- /dev/null
+++ b/gn_libs/logging.py
@@ -0,0 +1,41 @@
+import os
+import logging
+
+from flask import Flask
+
+logging.basicConfig(
+    encoding="utf-8",
+    format="%(asctime)s — %(filename)s:%(lineno)s — %(levelname)s: %(message)s")
+
+
+def __log_gunicorn__(app: Flask) -> Flask:
+    """Set up logging for the WSGI environment with GUnicorn"""
+    logger = logging.getLogger("gunicorn.error")
+    app.logger.handlers = logger.handlers
+    app.logger.setLevel(logger.level)
+    return app
+
+
+def __log_dev__(app: Flask) -> Flask:
+    """Set up logging for the development environment."""
+    root_logger = logging.getLogger()
+    root_logger.addHandler(stderr_handler)
+    root_logger.setLevel(
+        app.config.get("LOG_LEVEL", app.config.get("LOGLEVEL", "WARNING")))
+
+    return app
+
+
+def setup_logging(app: Flask) -> Flask:
+    """Set up logging for the application."""
+    software, *_version_and_comments = os.environ.get(
+        "SERVER_SOFTWARE", "").split('/')
+    return __log_gunicorn__(app) if bool(software) else __log_dev__(app)
+
+
+def setup_modules_logging(app_logger, modules: tuple[str, ...]):
+    """Setup module-level loggers to the same log-level as the application."""
+    loglevel = logging.getLevelName(app_logger.getEffectiveLevel())
+    for module in modules:
+        _logger = logging.getLogger(module)
+        _logger.setLevel(loglevel)