Skip to content

Commit

Permalink
Fix logger timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Nov 21, 2024
1 parent c528e5c commit 202a181
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions backend/src/xfd_django/xfd_django/middleware/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from starlette.requests import Request
from datetime import datetime


class LoggingMiddleware(BaseHTTPMiddleware):
def __init__(self, app):
super().__init__(app)
Expand All @@ -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
Expand All @@ -37,20 +36,20 @@ 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,
"originalURL": original_url,
"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

0 comments on commit 202a181

Please sign in to comment.