INNER CODE UNIT · Python
WinstonLoggingMiddleware
heymrun/heym · backend/app/main.py:257
class WinstonLoggingMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next) -> Response:
start_time = time.time()
client_ip = get_client_ip(request)
logger.info("request %s %s %s", request.method, request.url.path, client_ip)
try:
response = await call_next(request)
except Exception as exc:
duration = (time.time() - start_time) * 1000
logger.exception(
"error %s %s %sms %s %s",
request.method,
request.url.path,
f"{duration:.2f}",
client_ip,
str(exc),
)
raise