Skip to content

Commit

Permalink
Fix logger and bastion
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Nov 21, 2024
1 parent b5368e9 commit c528e5c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
3 changes: 2 additions & 1 deletion backend/src/xfd_django/xfd_api/tasks/bastion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
36 changes: 19 additions & 17 deletions backend/src/xfd_django/xfd_django/middleware/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -27,28 +30,27 @@ 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,
"originalURL": original_url,
"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

0 comments on commit c528e5c

Please sign in to comment.