From eaf127f93da11698b55e16a5ef53d110dae86b27 Mon Sep 17 00:00:00 2001 From: Colin B Date: Wed, 11 Dec 2024 17:37:38 +0000 Subject: [PATCH] move func and module to upper audit logger --- app/logger_config.py | 18 ++++++++++++++++++ app/main/middlewares/log_page_view.py | 2 -- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/logger_config.py b/app/logger_config.py index b682cc63..c1f587dd 100644 --- a/app/logger_config.py +++ b/app/logger_config.py @@ -1,3 +1,4 @@ +import inspect import json import logging @@ -6,17 +7,34 @@ class RequestFormatter(logging.Formatter): def format(self, record): + log_record = { "log_type": record.name, "timestamp": self.formatTime(record), "level": record.levelname, } + caller_function = "" + caller_module = "" + + for frame in inspect.stack(): + if "app.logger_config" not in str( + frame.frame.f_globals.get("__name__") + ): + if "app." in str(frame.frame.f_globals.get("__name__")): + caller_function = frame.function + caller_module = frame.frame.f_globals.get( + "__name__", "unknown" + ) + break + if has_request_context(): log_record.update( { "remote_addr": request.remote_addr, "url": request.url, + "caller_function": caller_function, + "caller_module": caller_module, } ) diff --git a/app/main/middlewares/log_page_view.py b/app/main/middlewares/log_page_view.py index 491b52c3..29179d38 100644 --- a/app/main/middlewares/log_page_view.py +++ b/app/main/middlewares/log_page_view.py @@ -13,8 +13,6 @@ def wrapper(*args, **kwargs): "user_id": user_id, "route": request.url, "method": request.method, - "caller_function": route_function.__name__, - "caller_module": route_function.__module__, } current_app.audit_logger.info(json.dumps(log_data)) return route_function(*args, **kwargs)