Skip to content

Commit

Permalink
fixup! Fix: Log request body (#6404)
Browse files Browse the repository at this point in the history
  • Loading branch information
achave11-ucsc committed Nov 21, 2024
1 parent ecf4d2b commit 1c2f71b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/azul/chalice.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,10 @@ def _log_body_msg(self, body_type: str, body: str | JSON | None):
len_msg = f' (total {n} bytes)'
if config.debug == 1:
body = body[:1024]
len_msg = f'{len_msg}, displaying initial 1024 bytes'
log.debug(http_body_log_message(body_type, body, verbatim=True) + len_msg + '.')
len_msg = f'{len_msg}, displaying initial 1024 bytes.'
log.debug(http_body_log_message(body_type, body, verbatim=True) + len_msg)
else:
log.info('With %s body size of %i bytes.', body_type, len(body))
log.info(http_body_log_message(body_type, body, size_only=True))
else:
log.info(http_body_log_message(body_type, None))

Expand Down
7 changes: 6 additions & 1 deletion src/azul/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def http_body_log_message(body_type: str,
body: bytes | bytearray | str | None,
*,
verbatim: bool = False,
size_only: bool = False,
) -> str:
if body is None:
return f'… without {body_type} body'
Expand All @@ -184,6 +185,10 @@ def http_body_log_message(body_type: str,
body = body.decode(errors='ignore')
else:
body = trunc_ellipses(body, max_len=128)
return f'… with {body_type} body {body!r}'
body_type_msg = f'… with {body_type} body'
if size_only:
return f'{body_type_msg} size of {len(body)} bytes'
else:
return f'{body_type_msg} {body!r}'
else:
return f'… with nonprintable body ({type(body)!r})'
14 changes: 7 additions & 7 deletions test/service/test_app_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def _head_response(self) -> str:
}
}
}
)[:1024] # … upto the first 1024 characters.
)

def test_request_logs(self):
for azul_debug in (0, 1, 2):
Expand Down Expand Up @@ -209,12 +209,12 @@ def test_request_logs(self):
) if not request_body else
(
INFO,
'With request body size of 47 bytes.'
'… with request body size of 47 bytes'
) if azul_debug == 0 else
(
DEBUG,
f'… with request body {request_body!r} (total 47 bytes)'
f'{", displaying initial 1024 bytes." if azul_debug == 1 else "."}'
f'{", displaying initial 1024 bytes." if azul_debug == 1 else ""}'
),
(
INFO,
Expand All @@ -238,15 +238,15 @@ def test_request_logs(self):
])
if azul_debug == 0:
self.assertEqual(last_log,
'With response body size of 9102 bytes.')
'… with response body size of 128 bytes')
elif azul_debug == 1:
self.assertEqual(last_log,
f'… with response body {self._head_response!r} '
f'… with response body {self._head_response[:1024]!r} '
f'(total 9102 bytes), displaying initial 1024 bytes.')
elif azul_debug == 2:
self.assertTrue(last_log.startswith(
f"… with response body \'{self._head_response}"))
self.assertTrue(last_log.endswith(' (total 9102 bytes).'))
f"… with response body \'{self._head_response[:1024]}"))
self.assertTrue(last_log.endswith(' (total 9102 bytes)'))
else:
assert False
self.assertEqual(level, last_log_level)
Expand Down
2 changes: 1 addition & 1 deletion test/test_app_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def fail():
'"1; mode=block", "Cache-Control": "no-store"}.')
self.assertEqual(
azul_log.output[4],
'INFO:azul.chalice:With response body size of 80 bytes.'
'INFO:azul.chalice:… with response body size of 80 bytes'
)


Expand Down

0 comments on commit 1c2f71b

Please sign in to comment.