From e3687f8a56ea0928d95a4554ec05046c378025b0 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 2 May 2025 09:46:50 -0500 Subject: PoC: Use independent module-level loggers with application Using flask.current_app.logger for logging in modules leads to the modules requiring an app context, even when the module itself could be used outside of the application. This commit provides a proof-of-concept (really, me finally putting in the effort to figure out logging) for how we could allow independent loggers at the module level, that can then be hooked up to the main logging in the system in question. --- uploader/__init__.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'uploader/__init__.py') diff --git a/uploader/__init__.py b/uploader/__init__.py index 69d34f7..f5f6641 100644 --- a/uploader/__init__.py +++ b/uploader/__init__.py @@ -55,6 +55,11 @@ def setup_logging(app: Flask) -> Flask: "SERVER_SOFTWARE", "").split('/') return __log_gunicorn__(app) if bool(software) else __log_dev__(app) +def setup_modules_logging(app_logger): + """Setup module-level loggers to the same log-level as the application.""" + loglevel = logging.getLevelName(app_logger.getEffectiveLevel()) + logging.getLogger("uploader.publications.models").setLevel(loglevel) + def create_app(config: dict = {}): """The application factory. @@ -88,6 +93,7 @@ def create_app(config: dict = {}): default_timeout=int(app.config["SESSION_FILESYSTEM_CACHE_TIMEOUT"])) setup_logging(app) + setup_modules_logging(app.logger) # setup jinja2 symbols app.add_template_global(lambda : request.url, name="request_url") -- cgit v1.2.3