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 Feb 5, 2025
1 parent 99b7006 commit e83b9f0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/azul/chalice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
4 changes: 3 additions & 1 deletion src/azul/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})'

0 comments on commit e83b9f0

Please sign in to comment.