From 202a1812b541f6e10dc23bea35547138db277b67 Mon Sep 17 00:00:00 2001 From: aloftus23 Date: Thu, 21 Nov 2024 06:22:19 -0500 Subject: [PATCH] Fix logger timestamp --- .../xfd_django/middleware/middleware.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/backend/src/xfd_django/xfd_django/middleware/middleware.py b/backend/src/xfd_django/xfd_django/middleware/middleware.py index b3c7832d..96e959bf 100644 --- a/backend/src/xfd_django/xfd_django/middleware/middleware.py +++ b/backend/src/xfd_django/xfd_django/middleware/middleware.py @@ -4,7 +4,6 @@ from starlette.requests import Request from datetime import datetime - class LoggingMiddleware(BaseHTTPMiddleware): def __init__(self, app): super().__init__(app) @@ -23,7 +22,7 @@ def _configure_logger(self): return logger async def dispatch(self, request: Request, call_next): - # Extract headers and other relevant information + # Extract relevant request details headers = dict(request.headers) method = request.method path = request.url.path @@ -37,10 +36,7 @@ async def dispatch(self, request: Request, call_next): # Default to "undefined" for userEmail if not provided user_email = request.state.user_email if hasattr(request.state, "user_email") else "undefined" - # Proceed with the request and capture the response - response = await call_next(request) - - # Log details in the desired format + # Prepare log details log_info = { "httpMethod": method, "protocol": protocol, @@ -48,9 +44,12 @@ async def dispatch(self, request: Request, call_next): "path": path, "headers": headers, "userEmail": user_email, - "requestId": request_id, - "statusCode": response.status_code, + "timestamp": datetime.utcnow().isoformat(), } - # Log in the desired format - self.logger.info("", extra={"request_id": request_id, "asctime": datetime.utcnow().isoformat(), "message": log_info}) + + # Log the request + self.logger.info(log_info) + + # Proceed with the request + response = await call_next(request) return response