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

@ -46,14 +46,22 @@ async def log_all_requests(request: Request, handler: Handler) -> StreamResponse
} }
} }
try:
try: try:
if request.content_length: if request.content_length:
if request.content_type == "application/json": if request.content_type == "application/json":
to_log["request"]["body"] = await request.json() to_log["request"]["body"] = await request.json()
else: else:
to_log["request"]["body"] = {h for h in await request.post()} 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)
try:
if response is None: if response is None:
_LOGGER.warning( # type:ignore[unreachable] _LOGGER.warning( # type:ignore[unreachable]
"Response was null!" "Response was null!"
@ -74,16 +82,15 @@ async def log_all_requests(request: Request, handler: Handler) -> StreamResponse
to_log["response"]["body"] = response.text to_log["response"]["body"] = response.text
return response return response
except Exception:
_LOGGER.exception(
"An exception occurred during logging the response", exc_info=True
)
raise
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))

View file

@ -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(