diff --git a/backend/src/xfd_django/xfd_api/tasks/bastion.py b/backend/src/xfd_django/xfd_api/tasks/bastion.py index d03bc736..2d520114 100644 --- a/backend/src/xfd_django/xfd_api/tasks/bastion.py +++ b/backend/src/xfd_django/xfd_api/tasks/bastion.py @@ -39,7 +39,8 @@ def handle_db_query(query): cursor.execute(query) result = cursor.fetchall() - return {"statusCode": 200, "body": {"result": result}} + print(str(result)) + return {"statusCode": 200, "body": str(result)} def handle_es_query(query): diff --git a/backend/src/xfd_django/xfd_django/middleware/middleware.py b/backend/src/xfd_django/xfd_django/middleware/middleware.py index dceddcc6..b3c7832d 100644 --- a/backend/src/xfd_django/xfd_django/middleware/middleware.py +++ b/backend/src/xfd_django/xfd_django/middleware/middleware.py @@ -2,6 +2,8 @@ from pythonjsonlogger import jsonlogger from starlette.middleware.base import BaseHTTPMiddleware from starlette.requests import Request +from datetime import datetime + class LoggingMiddleware(BaseHTTPMiddleware): def __init__(self, app): @@ -10,13 +12,14 @@ def __init__(self, app): def _configure_logger(self): logger = logging.getLogger("fastapi") - log_handler = logging.StreamHandler() - formatter = jsonlogger.JsonFormatter( - '%(levelname)s RequestId: %(request_id)s %(asctime)s Request Info: %(message)s' - ) - log_handler.setFormatter(formatter) - logger.addHandler(log_handler) - logger.setLevel(logging.INFO) + if not logger.handlers: # Avoid duplicate handlers + log_handler = logging.StreamHandler() + formatter = jsonlogger.JsonFormatter( + '%(levelname)s RequestId: %(request_id)s %(asctime)s Request Info: %(message)s' + ) + log_handler.setFormatter(formatter) + logger.addHandler(log_handler) + logger.setLevel(logging.INFO) return logger async def dispatch(self, request: Request, call_next): @@ -27,14 +30,17 @@ async def dispatch(self, request: Request, call_next): protocol = request.url.scheme original_url = str(request.url) - # Get request ID from scope + # Get request ID from scope or set it to "undefined" aws_context = request.scope.get("aws.context", None) request_id = getattr(aws_context, "aws_request_id", "undefined") if aws_context else "undefined" # Default to "undefined" for userEmail if not provided user_email = request.state.user_email if hasattr(request.state, "user_email") else "undefined" - # Log the request details + # Proceed with the request and capture the response + response = await call_next(request) + + # Log details in the desired format log_info = { "httpMethod": method, "protocol": protocol, @@ -42,13 +48,9 @@ async def dispatch(self, request: Request, call_next): "path": path, "headers": headers, "userEmail": user_email, - "requestId": request_id + "requestId": request_id, + "statusCode": response.status_code, } - - # Proceed with the request and capture the response - response = await call_next(request) - log_info["statusCode"] = response.status_code - - # Log response with updated status code - self.logger.info(log_info) + # Log in the desired format + self.logger.info("", extra={"request_id": request_id, "asctime": datetime.utcnow().isoformat(), "message": log_info}) return response