Some checks failed
Integration Tests / integration-tests (push) Failing after 20s
Integration Tests / performance-tests (push) Has been skipped
Service Adapters (Python FastAPI) / test (3.11) (push) Failing after 24s
Service Adapters (Python FastAPI) / test (3.12) (push) Failing after 25s
Service Adapters (Python FastAPI) / test (3.13) (push) Failing after 25s
Service Adapters (Python FastAPI) / build (push) Has been skipped
### Summary of Changes - Removed unnecessary whitespace and standardized formatting in multiple files, including `main.py`, `logging_middleware.py`, `general.py`, and various health checker implementations. - Enhanced readability and maintainability of the codebase by ensuring consistent formatting practices. ### Expected Results - Improved code clarity, making it easier for developers to read and understand the service adapters' code. - Streamlined the codebase, facilitating future updates and maintenance.
66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
"""
|
|
Logging Middleware
|
|
|
|
This module provides custom logging middleware for FastAPI requests
|
|
to ensure consistent logging format with application logs.
|
|
"""
|
|
|
|
import time
|
|
from typing import Callable
|
|
|
|
from fastapi import Request, Response
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
|
|
from services.logging_config import get_request_logger
|
|
|
|
logger = get_request_logger()
|
|
|
|
|
|
class LoggingMiddleware(BaseHTTPMiddleware):
|
|
"""Custom logging middleware for unified request logging."""
|
|
|
|
async def dispatch(self, request: Request, call_next: Callable) -> Response:
|
|
"""
|
|
Log each request with unified formatting.
|
|
|
|
Args:
|
|
request: The incoming request
|
|
call_next: The next middleware/handler in the chain
|
|
|
|
Returns:
|
|
The response
|
|
"""
|
|
# Start timing
|
|
start_time = time.time()
|
|
|
|
# Extract request information
|
|
method = request.method
|
|
url_path = request.url.path
|
|
client_ip = request.client.host if request.client else "unknown"
|
|
user_agent = request.headers.get("user-agent", "unknown")
|
|
|
|
# Log request start
|
|
logger.info(f"Request started: {method} {url_path} from {client_ip} " f"(User-Agent: {user_agent})")
|
|
|
|
try:
|
|
# Process the request
|
|
response = await call_next(request)
|
|
|
|
# Calculate processing time
|
|
process_time = time.time() - start_time
|
|
|
|
# Log successful response
|
|
logger.info(f"Request completed: {method} {url_path} -> " f"{response.status_code} in {process_time:.3f}s")
|
|
|
|
return response
|
|
|
|
except Exception as e:
|
|
# Calculate processing time for failed requests
|
|
process_time = time.time() - start_time
|
|
|
|
# Log error
|
|
logger.error(f"Request failed: {method} {url_path} -> " f"Exception: {str(e)} in {process_time:.3f}s")
|
|
|
|
# Re-raise the exception
|
|
raise
|