diff options
author | Frederick Muriuki Muriithi | 2024-08-08 15:29:05 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-08-08 15:29:44 -0500 |
commit | 494f138387216757b61b8c2d3b4f98ecc197d18c (patch) | |
tree | fff9104316a7e32406e71af441df4597db45af42 | |
parent | 0087e2b063db4ebc4990e94f5e0ed2253d8b1da4 (diff) | |
download | gn-uploader-494f138387216757b61b8c2d3b4f98ecc197d18c.tar.gz |
Set up logging for the application.
Set up logging for the application to help with debugging issues.
-rw-r--r-- | uploader/__init__.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/uploader/__init__.py b/uploader/__init__.py index 266495c..2d731af 100644 --- a/uploader/__init__.py +++ b/uploader/__init__.py @@ -1,5 +1,6 @@ """The Quality-Control Web Application entry point""" import os +import sys import logging from pathlib import Path @@ -24,6 +25,33 @@ def override_settings_with_envvars( app.config[setting] = os.environ.get(setting) or app.config[setting] +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.""" + stderr_handler = logging.StreamHandler(stream=sys.stderr) + app.logger.addHandler(stderr_handler) + + root_logger = logging.getLogger() + root_logger.addHandler(stderr_handler) + root_logger.setLevel(app.config["LOG_LEVEL"]) + + 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 create_app(): """The application factory""" app = Flask(__name__) @@ -42,6 +70,8 @@ def create_app(): # Silently ignore secrets if the file does not exist. app.config.from_pyfile(secretsfile) + setup_logging(app) + # setup jinja2 symbols app.add_template_global(lambda : request.url, name="request_url") app.add_template_global(authserver_authorise_uri) |