From 1c2f71bb6de2cb5c12e1467694ec342e952d11a0 Mon Sep 17 00:00:00 2001 From: Abraham Chavez Date: Thu, 21 Nov 2024 09:28:58 -0800 Subject: [PATCH] fixup! Fix: Log request body (#6404) --- src/azul/chalice.py | 6 +++--- src/azul/logging.py | 7 ++++++- test/service/test_app_logging.py | 14 +++++++------- test/test_app_logging.py | 2 +- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/azul/chalice.py b/src/azul/chalice.py index 4d4c3f8cc..30cc07157 100644 --- a/src/azul/chalice.py +++ b/src/azul/chalice.py @@ -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)) diff --git a/src/azul/logging.py b/src/azul/logging.py index d0b480f67..01e9a0bf7 100644 --- a/src/azul/logging.py +++ b/src/azul/logging.py @@ -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' @@ -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})' diff --git a/test/service/test_app_logging.py b/test/service/test_app_logging.py index e7533a222..78eddfdf3 100644 --- a/test/service/test_app_logging.py +++ b/test/service/test_app_logging.py @@ -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): @@ -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, @@ -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) diff --git a/test/test_app_logging.py b/test/test_app_logging.py index e105df77b..200dd15a1 100644 --- a/test/test_app_logging.py +++ b/test/test_app_logging.py @@ -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' )