From e83b9f0ed67755949970b1df0e1176c6a8d82167 Mon Sep 17 00:00:00 2001 From: Abraham Chavez Date: Tue, 4 Feb 2025 19:55:41 -0800 Subject: [PATCH] fixup! Fix: Log request body (#6404) --- src/azul/chalice.py | 10 +++++----- src/azul/logging.py | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/azul/chalice.py b/src/azul/chalice.py index 9b89c3844..6b043ef10 100644 --- a/src/azul/chalice.py +++ b/src/azul/chalice.py @@ -477,22 +477,22 @@ def _log_request(self): context['httpMethod'], context['path'], json.dumps(request_info, cls=self._LogJSONEncoder)) - self._log_body_msg('request', self.current_request.json_body) + self._log_body('request', self.current_request.json_body) def _log_response(self, response): log.info('Returning %i response with headers %s.', response.status_code, json.dumps(response.headers, cls=self._LogJSONEncoder)) - self._log_body_msg('response', response.body) + self._log_body('response', response.body) - def _log_body_msg(self, body_type: BodyType, body: str | JSON | None): + def _log_body(self, body_type: BodyType, body: str | JSON | None): if body is None: log.info(http_body_log_message(body_type, None)) else: if isinstance(body, Mapping): body = json.dumps(body) if log.isEnabledFor(logging.DEBUG): - body_log = http_body_log_message(body_type, body, verbatim=config.debug == 2) - log.debug(body_log) + message = http_body_log_message(body_type, body, verbatim=config.debug > 1) + log.debug(message) else: log.info('… with %s body size of %i bytes', body_type, len(body)) diff --git a/src/azul/logging.py b/src/azul/logging.py index 2279f8868..0d5c9a2ce 100644 --- a/src/azul/logging.py +++ b/src/azul/logging.py @@ -184,9 +184,11 @@ def http_body_log_message(body_type: BodyType, if verbatim: if isinstance(body, (bytes, bytearray)): body = body.decode(errors='ignore') + qualify_log = 'all ' else: # https://github.com/python/typing/discussions/1911 body = trunc_ellipses(body, max_len=prefix_len) # type: ignore[type-var] - return f'… with {body_type} body {body!r} (total {body_len} bytes)' + qualify_log = f'first {prefix_len}/' + return f'… with {qualify_log}{body_len} bytes of the {body_type} body {body!r}' else: return f'… with nonprintable body ({type(body)!r})'