Skip to content

Commit

Permalink
🐛 [#34] Properly log data for error responses
Browse files Browse the repository at this point in the history
the handler previously did not log `status_code`, `response_ms` and `res_headers` because truthy checks on `Response` evaluate to false for error responses
  • Loading branch information
stevenbal committed Feb 8, 2024
1 parent 978b593 commit 8611e8f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
16 changes: 10 additions & 6 deletions log_outgoing_requests/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,18 @@ def emit(self, record: AnyLogRecord):
"url": request.url if request else "(unknown)",
"hostname": parsed_url.netloc if parsed_url else "(unknown)",
"params": parsed_url.params if parsed_url else "(unknown)",
"status_code": response.status_code if response else None,
"status_code": response.status_code if response is not None else None,
"method": request.method if request else "(unknown)",
"timestamp": timestamp,
"response_ms": int(response.elapsed.total_seconds() * 1000)
if response
else 0,
"response_ms": (
int(response.elapsed.total_seconds() * 1000)
if response is not None
else 0
),
"req_headers": self.format_headers(scrubbed_req_headers),
"res_headers": self.format_headers(response.headers if response else {}),
"res_headers": self.format_headers(
response.headers if response is not None else {}
),
"trace": "\n".join(format_exception(exception)) if exception else "",
}

Expand All @@ -121,7 +125,7 @@ def emit(self, record: AnyLogRecord):

# check response
if (
response
response is not None
and (
processed_response_body := process_body(response, config)
).allow_saving_to_db
Expand Down
12 changes: 6 additions & 6 deletions log_outgoing_requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ def check_content_type(content_type: str) -> bool:
For patterns containing a wildcard ("text/*"), check if `content_type.pattern`
is a substring of any pattern contained in the list.
"""
allowed_content_types: Iterable[
ContentType
] = settings.LOG_OUTGOING_REQUESTS_CONTENT_TYPES
allowed_content_types: Iterable[ContentType] = (
settings.LOG_OUTGOING_REQUESTS_CONTENT_TYPES
)
regular_patterns = [
item.pattern for item in allowed_content_types if not item.pattern.endswith("*")
]
Expand All @@ -133,9 +133,9 @@ def get_default_encoding(content_type_pattern: str) -> str:
"""
Get the default encoding for the `ContentType` with the associated pattern.
"""
allowed_content_types: Iterable[
ContentType
] = settings.LOG_OUTGOING_REQUESTS_CONTENT_TYPES
allowed_content_types: Iterable[ContentType] = (
settings.LOG_OUTGOING_REQUESTS_CONTENT_TYPES
)

regular_types = [
item for item in allowed_content_types if not item.pattern.endswith("*")
Expand Down

0 comments on commit 8611e8f

Please sign in to comment.