aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-11-04 16:54:45 -0600
committerFrederick Muriuki Muriithi2024-11-04 16:54:45 -0600
commit95e65819fd4155f539734a5de2eb108d232703a6 (patch)
tree20d6aba7c2465a5d1e7749edec7d252bae5ce70c
parentfe3ba6d5c6c6acafdfb3ae5dacc2c95d997ec761 (diff)
downloadgn-auth-95e65819fd4155f539734a5de2eb108d232703a6.tar.gz
Move logging setup functions to gn_auth.__init__.py module
To ensure that logging will always be setup correctly, move the functions into the __init__.py module and call it within the create_app(..) function as before.
-rw-r--r--gn_auth/__init__.py37
-rw-r--r--gn_auth/wsgi.py31
2 files changed, 34 insertions, 34 deletions
diff --git a/gn_auth/__init__.py b/gn_auth/__init__.py
index b695ebf..658f034 100644
--- a/gn_auth/__init__.py
+++ b/gn_auth/__init__.py
@@ -1,6 +1,7 @@
"""Application initialisation module."""
import os
import sys
+import logging
from pathlib import Path
from typing import Optional, Callable
@@ -52,10 +53,38 @@ def load_secrets_conf(app: Flask) -> None:
app.config.from_pyfile(secretsfile)
-def create_app(
- config: Optional[dict] = None,
- setup_logging: Callable[[Flask], None] = lambda appl: None
-) -> Flask:
+def dev_loggers(appl: Flask) -> None:
+ """Setup the logging handlers."""
+ stderr_handler = logging.StreamHandler(stream=sys.stderr)
+ appl.logger.addHandler(stderr_handler)
+
+ root_logger = logging.getLogger()
+ root_logger.addHandler(stderr_handler)
+ root_logger.setLevel(appl.config["LOGLEVEL"])
+
+
+def gunicorn_loggers(appl: Flask) -> None:
+ """Use gunicorn logging handlers for the application."""
+ logger = logging.getLogger("gunicorn.error")
+ appl.logger.handlers = logger.handlers
+ appl.logger.setLevel(logger.level)
+
+
+def setup_logging(appl: Flask) -> Callable[[Flask], None]:
+ """
+ Setup the loggers according to the WSGI server used to run the application.
+ """
+ # https://datatracker.ietf.org/doc/html/draft-coar-cgi-v11-03#section-4.1.17
+ # https://wsgi.readthedocs.io/en/latest/proposals-2.0.html#making-some-keys-required
+ # https://peps.python.org/pep-3333/#id4
+ software, *_version_and_comments = os.environ.get(
+ "SERVER_SOFTWARE", "").split('/')
+ if bool(software):
+ return gunicorn_loggers(appl)
+ return dev_loggers(appl)
+
+
+def create_app(config: Optional[dict] = None) -> Flask:
"""Create and return a new flask application."""
app = Flask(__name__)
diff --git a/gn_auth/wsgi.py b/gn_auth/wsgi.py
index bb8abd2..4904da3 100644
--- a/gn_auth/wsgi.py
+++ b/gn_auth/wsgi.py
@@ -3,7 +3,6 @@ import os
import sys
import uuid
import json
-import logging
from math import ceil
from pathlib import Path
from typing import Callable
@@ -24,35 +23,7 @@ from gn_auth.auth.authorisation.users.admin.models import make_sys_admin
from scripts import register_sys_admin as rsysadm# type: ignore[import]
-def dev_loggers(appl: Flask) -> None:
- """Setup the logging handlers."""
- stderr_handler = logging.StreamHandler(stream=sys.stderr)
- appl.logger.addHandler(stderr_handler)
-
- root_logger = logging.getLogger()
- root_logger.addHandler(stderr_handler)
- root_logger.setLevel(appl.config["LOGLEVEL"])
-
-
-def gunicorn_loggers(appl: Flask) -> None:
- """Use gunicorn logging handlers for the application."""
- logger = logging.getLogger("gunicorn.error")
- appl.logger.handlers = logger.handlers
- appl.logger.setLevel(logger.level)
-
-
-def setup_loggers() -> Callable[[Flask], None]:
- """
- Setup the loggers according to the WSGI server used to run the application.
- """
- # https://datatracker.ietf.org/doc/html/draft-coar-cgi-v11-03#section-4.1.17
- # https://wsgi.readthedocs.io/en/latest/proposals-2.0.html#making-some-keys-required
- # https://peps.python.org/pep-3333/#id4
- software, *_version_and_comments = os.environ.get(
- "SERVER_SOFTWARE", "").split('/')
- return gunicorn_loggers if bool(software) else dev_loggers
-
-app = create_app(setup_logging=setup_loggers())
+app = create_app()
##### BEGIN: CLI Commands #####