Log response status in request_logging (#3747)
This commit is contained in:
committed by
GitHub
parent
a22260f871
commit
d2d7b8e4b0
@@ -66,12 +66,30 @@ async def log_raw_request_middleware(request: Request, call_next: Callable[[Requ
|
|||||||
body_text = _sanitize_body(request, body_bytes, request.headers.get("content-type"))
|
body_text = _sanitize_body(request, body_bytes, request.headers.get("content-type"))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return await call_next(request)
|
response = await call_next(request)
|
||||||
finally:
|
|
||||||
LOG.info(
|
if response.status_code >= 500:
|
||||||
|
log_method = LOG.error
|
||||||
|
elif response.status_code >= 400:
|
||||||
|
log_method = LOG.warning
|
||||||
|
else:
|
||||||
|
log_method = LOG.info
|
||||||
|
log_method(
|
||||||
|
"api.raw_request",
|
||||||
|
method=http_method,
|
||||||
|
path=url_path,
|
||||||
|
status_code=response.status_code,
|
||||||
|
body=body_text,
|
||||||
|
headers=sanitized_headers,
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
except Exception:
|
||||||
|
LOG.error(
|
||||||
"api.raw_request",
|
"api.raw_request",
|
||||||
method=http_method,
|
method=http_method,
|
||||||
path=url_path,
|
path=url_path,
|
||||||
body=body_text,
|
body=body_text,
|
||||||
headers=sanitized_headers,
|
headers=sanitized_headers,
|
||||||
|
exc_info=True,
|
||||||
)
|
)
|
||||||
|
raise
|
||||||
|
|||||||
Reference in New Issue
Block a user