about summary refs log tree commit diff
diff options
context:
space:
mode:
-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)