Spaces:
Running
Running
File size: 2,068 Bytes
a8a2cf5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import logging
import time
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response
from fastapi import FastAPI
# --- 1. SETUP LOGGER (Do this ONCE, globally) ---
logger = logging.getLogger("my_app_logger")
logger.setLevel(logging.INFO)
# Create file handler once
file_handler = logging.FileHandler('logging.txt')
formatter = logging.Formatter('%(asctime)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
class RequestLoggingMiddleware(BaseHTTPMiddleware):
"""
Logs every request that comes in Middleware class needs
- dispatch method that handles the request
"""
async def dispatch(self, request: Request, call_next) -> Response:
# --- 2. BEFORE REQUEST ---
start_time = time.time()
# Log that we started (Optional)
logger.info(f"Incoming: {request.method} {request.url.path}")
# --- 3. PASS TO ENDPOINT ---
# This jumps to your actual API function and waits for it to return
response = await call_next(request)
# --- 4. AFTER REQUEST ---
process_time = (time.time() - start_time) * 1000 # Calculate duration
# Log the result
logger.info(
f"Completed: {request.method} {request.url.path} "
f"- Status: {response.status_code} "
f"- Duration: {process_time:.2f}ms"
)
return response
# # from fastapi import FastAPI
# # from fastapi.middleware.cors import CORSMiddleware
# from src.api.middleware.logging import RequestLoggingMiddleware
# app = FastAPI()
# # Add middlewares
# # ORDER MATTERS! Last added = First to run
# # 1. CORS (Cross-Origin Resource Sharing)
# app.add_middleware(
# CORSMiddleware,
# allow_origins=["http://localhost:3000"], # Frontend URL
# allow_credentials=True,
# allow_methods=["*"],
# allow_headers=["*"],
# )
# # 2. Our custom logging middleware
# app.add_middleware(RequestLoggingMiddleware) |