Skip to content

Commit 0043d94

Browse files
committed
✨ Add headers field to apicall response log
1 parent 55ba6a2 commit 0043d94

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

fastapi_and_logging/http_clients/aiohttp.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@ async def default_request_hook(self, session, trace_config_ctx, params):
4343

4444
async def default_response_hook(self, session, trace_config_ctx, params):
4545
response = params.response
46-
request_data = trace_config_ctx.trace_request_ctx.pop(
47-
"request_data",
48-
)
46+
request_data, request_id = {}, {}
47+
if trace_config_ctx.trace_request_ctx:
48+
request_data = trace_config_ctx.trace_request_ctx.pop(
49+
"request_data", None
50+
)
51+
request_id = trace_config_ctx.trace_request_ctx.pop(
52+
"request_id",
53+
)
4954
response_data = await response.read()
50-
5155
extra_data = {
52-
"request_id": trace_config_ctx.trace_request_ctx.pop(
53-
"request_id",
54-
),
56+
"request_id": request_id,
5557
"method": response.method,
5658
"url": str(response.url),
5759
"status_code": response.status,
@@ -60,6 +62,7 @@ async def default_response_hook(self, session, trace_config_ctx, params):
6062
response_data, self.response_max_len
6163
),
6264
"trace_request_ctx": trace_config_ctx.trace_request_ctx,
65+
"headers": dict(params.headers),
6366
}
6467

6568
get_apicall_logger(

fastapi_and_logging/http_clients/httpx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def response_hook(self, response: httpx.Response) -> None:
6363
"status_code": response.status_code,
6464
"request_data": str(response.request.content)[:request_max_len],
6565
"response_data": str(response.read())[:response_max_len],
66+
"headers": dict(response.request.headers),
6667
}
6768

6869
if callable(HTTPXClient._response_hook):
@@ -102,6 +103,7 @@ async def response_hook(self, response: httpx.Response) -> None:
102103
"status_code": response.status_code,
103104
"request_data": str(response.request.content)[:request_max_len],
104105
"response_data": str(await response.aread())[:response_max_len],
106+
"headers": dict(response.request.headers),
105107
}
106108

107109
if callable(HTTPXAsyncClient._response_hook):

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fastapi-and-logging"
3-
version = "0.0.7"
3+
version = "0.0.8"
44
description = "FastAPI-and-Logging simplifies log handling, allowing for effective organization, tracking, and analysis of logs in FastAPI applications, aiding in debugging and issue resolut"
55
authors = ["Saeid Noormohammadi <heysaeid92@gmail.com>"]
66
license = "MIT"
@@ -71,4 +71,4 @@ ignore = [
7171

7272
[tool.ruff.pyupgrade]
7373
# Preserve types, even if a file imports `from __future__ import annotations`.
74-
keep-runtime-typing = true
74+
keep-runtime-typing = true

0 commit comments

Comments
 (0)