improve logging

This commit is contained in:
Robert Resch 2022-08-24 22:41:06 +02:00
parent 36d7c468fa
commit 496c75b05f
2 changed files with 38 additions and 33 deletions

View file

@ -47,43 +47,50 @@ async def log_all_requests(request: Request, handler: Handler) -> StreamResponse
}
try:
if request.content_length:
if request.content_type == "application/json":
to_log["request"]["body"] = await request.json()
else:
to_log["request"]["body"] = {h for h in await request.post()}
try:
if request.content_length:
if request.content_type == "application/json":
to_log["request"]["body"] = await request.json()
else:
to_log["request"]["body"] = {h for h in await request.post()}
except Exception:
_LOGGER.exception(
"An exception occurred during logging the request.", exc_info=True
)
raise
response = await handler(request)
if response is None:
_LOGGER.warning( # type:ignore[unreachable]
"Response was null!"
try:
if response is None:
_LOGGER.warning( # type:ignore[unreachable]
"Response was null!"
)
_LOGGER.warning(json.dumps(to_log, cls=CustomEncoder))
raise HTTPNoContent
to_log["response"] = {
"status": f"{response.status}",
"headers": {h for h in response.headers.items()},
}
if isinstance(response, Response) and response.body:
assert response.text
if response.content_type == "application/json":
to_log["response"]["body"] = json.loads(response.text)
elif response.content_type.startswith("text"):
to_log["response"]["body"] = response.text
return response
except Exception:
_LOGGER.exception(
"An exception occurred during logging the response", exc_info=True
)
_LOGGER.warning(json.dumps(to_log, cls=CustomEncoder))
raise HTTPNoContent
to_log["response"] = {
"status": f"{response.status}",
"headers": {h for h in response.headers.items()},
}
if isinstance(response, Response) and response.body:
assert response.text
if response.content_type == "application/json":
to_log["response"]["body"] = json.loads(response.text)
elif response.content_type.startswith("text"):
to_log["response"]["body"] = response.text
return response
raise
except web.HTTPNotFound:
_LOGGER.debug(f"Request path {request.raw_path} not found")
raise
except Exception:
_LOGGER.exception(
"An exception occurred in the logging middleware.", exc_info=True
)
raise
finally:
_LOGGER.debug(json.dumps(to_log, cls=CustomEncoder))

View file

@ -333,10 +333,8 @@ class WebServer:
)
else:
# handle json
jdata = read_body.decode("utf8")
jdata = json.loads(jdata)
async with session.request(
request.method, request.url, json=jdata
request.method, request.url, json=request.json()
) as resp:
response = await resp.text()
proxymodelog.info(