improve logging
This commit is contained in:
parent
36d7c468fa
commit
496c75b05f
2 changed files with 38 additions and 33 deletions
|
|
@ -47,43 +47,50 @@ async def log_all_requests(request: Request, handler: Handler) -> StreamResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if request.content_length:
|
try:
|
||||||
if request.content_type == "application/json":
|
if request.content_length:
|
||||||
to_log["request"]["body"] = await request.json()
|
if request.content_type == "application/json":
|
||||||
else:
|
to_log["request"]["body"] = await request.json()
|
||||||
to_log["request"]["body"] = {h for h in await request.post()}
|
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)
|
response = await handler(request)
|
||||||
if response is None:
|
|
||||||
_LOGGER.warning( # type:ignore[unreachable]
|
try:
|
||||||
"Response was null!"
|
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
|
||||||
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 web.HTTPNotFound:
|
except web.HTTPNotFound:
|
||||||
_LOGGER.debug(f"Request path {request.raw_path} not found")
|
_LOGGER.debug(f"Request path {request.raw_path} not found")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
except Exception:
|
|
||||||
_LOGGER.exception(
|
|
||||||
"An exception occurred in the logging middleware.", exc_info=True
|
|
||||||
)
|
|
||||||
raise
|
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
_LOGGER.debug(json.dumps(to_log, cls=CustomEncoder))
|
_LOGGER.debug(json.dumps(to_log, cls=CustomEncoder))
|
||||||
|
|
|
||||||
|
|
@ -333,10 +333,8 @@ class WebServer:
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# handle json
|
# handle json
|
||||||
jdata = read_body.decode("utf8")
|
|
||||||
jdata = json.loads(jdata)
|
|
||||||
async with session.request(
|
async with session.request(
|
||||||
request.method, request.url, json=jdata
|
request.method, request.url, json=request.json()
|
||||||
) as resp:
|
) as resp:
|
||||||
response = await resp.text()
|
response = await resp.text()
|
||||||
proxymodelog.info(
|
proxymodelog.info(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue