SOARDO
Software Engineer (SDE)fastapiintermediate3 interactive MCQs in app

Custom Middleware for Logging & Timing

timing & request-id header injectionnever log tokens/PII in request middlewareBaseHTTPMiddleware buffering vs pure ASGI

Custom HTTP middleware (`@app.middleware("http")` or pure ASGI middleware) wraps every request/response for timing (`X-Process-Time`), correlation `request_id` context propagation, and structured request logging (`method`, `path`, `status`, `latency_ms`—NEVER logging `Authorization` headers or passwords!).

capturing `t0 = perf_counter()` before `response = await call_next(request)` in `try/finally` emits `X-Response-Time-Ms` and structured latency logs on every path!
reading `await request.body()` inside naive `@app.middleware("http")` consumes the stream cleanly unless carefully restored (or breaks streaming uploads/SSE due to `BaseHTTPMiddleware` buffering—use pure ASGI middleware for streaming!).
FastAPI — Custom Middleware (Official)read ↗Custom Middleware for Logging & Timing — Applied Practicepractice ↗

Practice Custom Middleware for Logging & Timing with 3 graded MCQs & written feedback.

Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.

Test yourself free on SOARDO →View full Software Engineer roadmap

More Software Engineer Study Guides