aboutsummaryrefslogtreecommitdiff
path: root/wsgi.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-04-10 10:55:51 +0300
committerFrederick Muriuki Muriithi2024-04-10 10:55:51 +0300
commitacdbc30fd8d12d25f48a2ea839a6c2eb91527b95 (patch)
treeee3bad7b202b76bc162403c220d054779151788b /wsgi.py
parent061a40c14f3adf39c794aa0ddace4835f9282e9b (diff)
downloadgn-uploader-acdbc30fd8d12d25f48a2ea839a6c2eb91527b95.tar.gz
logging: Set up logging in wsgi.py
Make flask use the gunicorn loggers when run under gunicorn, otherwise, use our custom logging. Putting the logging setup inside `create_app(…)` would cause each worker to override the gunicorn loggers, meaning we were not receiving the logs, especially for `debug(…)` calls.
Diffstat (limited to 'wsgi.py')
-rw-r--r--wsgi.py28
1 files changed, 26 insertions, 2 deletions
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()