Skip to content

Commit

Permalink
move func and module to upper audit logger
Browse files Browse the repository at this point in the history
  • Loading branch information
colinbowen committed Dec 11, 2024
1 parent c754df2 commit eaf127f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 18 additions & 0 deletions app/logger_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import json
import logging

Expand All @@ -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,
}
)

Expand Down
2 changes: 0 additions & 2 deletions app/main/middlewares/log_page_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit eaf127f

Please sign in to comment.