From 7647579155dd0c26774c915edeba124651153c75 Mon Sep 17 00:00:00 2001 From: IgorVolochay Date: Tue, 1 Sep 2026 11:24:43 +0300 Subject: [PATCH] Add client_ip to logs --- app/middleware.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/middleware.py b/app/middleware.py index c82bfaf..764c889 100644 --- a/app/middleware.py +++ b/app/middleware.py @@ -38,10 +38,17 @@ class RequestLoggingMiddleware(BaseHTTPMiddleware): duration_ms = round((time.perf_counter() - start) * 1000, 1) status = response.status_code + client_ip = request.headers.get("X-Forwarded-For") + if client_ip: + client_ip = client_ip.split(",")[0].strip() + else: + client_ip = request.headers.get("X-Real-IP") or (request.client.host if request.client else "unknown") + base_fields = { "method": request.method, "path": request.url.path, "query": str(request.query_params) or None, + "client_ip": client_ip, "status": status, "duration_ms": duration_ms, }