aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--qc_app/__init__.py12
-rw-r--r--wsgi.py28
2 files changed, 26 insertions, 14 deletions
diff --git a/qc_app/__init__.py b/qc_app/__init__.py
index c9756a1..88e65be 100644
--- a/qc_app/__init__.py
+++ b/qc_app/__init__.py
@@ -1,9 +1,7 @@
"""The Quality-Control Web Application entry point"""
import os
-import sys
import logging
from pathlib import Path
-from logging import StreamHandler
from flask import Flask
@@ -20,15 +18,6 @@ def override_settings_with_envvars(
for setting in (key for key in app.config if key not in ignore):
app.config[setting] = os.environ.get(setting) or app.config[setting]
-def setup_logging(app: Flask):
- """Setup application logging"""
- handler_stderr = StreamHandler(stream=sys.stderr)
- app.logger.addHandler(handler_stderr)
-
- rootlogger = logging.getLogger()
- rootlogger.addHandler(handler_stderr)
- rootlogger.setLevel(app.config["LOG_LEVEL"])
-
def create_app():
"""The application factory"""
app = Flask(__name__)
@@ -38,7 +27,6 @@ def create_app():
app.config.from_envvar("QCAPP_CONF") # Override defaults with instance path
override_settings_with_envvars(app, ignore=tuple())
- setup_logging(app)
if "QCAPP_SECRETS" in os.environ:
app.config.from_envvar("QCAPP_SECRETS")
diff --git a/wsgi.py b/wsgi.py
index ff24d78..9fb63cb 100644
--- a/wsgi.py
+++ b/wsgi.py
@@ -1,9 +1,13 @@
"""Run the application"""
+import sys
+from logging import getLogger, StreamHandler
+
+from flask import Flask
from qc_app import create_app
from qc_app.check_connections import check_db, check_redis
-def check_and_build_app():
+def check_and_build_app() -> Flask:
"""Setup the application for running."""
# Setup the app
appl = create_app()
@@ -13,8 +17,28 @@ def check_and_build_app():
check_redis(appl.config["REDIS_URL"])
return appl
+def setup_logging(appl: Flask):
+ """Setup application logging"""
+ loglevel = appl.config["LOG_LEVEL"].upper()
+
+ # Maybe call `logging.dictConfig(…)` here instead of all this stuff below
+ handler_stderr = StreamHandler(stream=sys.stderr)
+ appl.logger.addHandler(handler_stderr)
+
+ rootlogger = getLogger()
+ rootlogger.addHandler(handler_stderr)
+ rootlogger.setLevel(loglevel)
+
+ appl.logger.setLevel(loglevel)
+
app = check_and_build_app()
-if __name__ == "__main__":
+if __name__ != "__main__":# Running via gunicorn
+ gunicorn_logger = getLogger("gunicorn.error")
+ app.logger.handlers = gunicorn_logger.handlers
+ app.logger.setLevel(gunicorn_logger.level)#pylint: disable=[no-member]
+
+if __name__ == "__main__":# Running via flask
# Run the app
+ setup_logging(app)
app.run()