Skip to content

Commit 6f4f186

Browse files
Add easier debug logging for users (#277)
Co-Authored-By: Florimond Manca <florimond.manca@gmail.com>
1 parent 33032df commit 6f4f186

9 files changed

Lines changed: 172 additions & 1 deletion

File tree

docs/environment_variables.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Environment Variables
2+
=====================
3+
4+
The HTTPX library can be configured via environment variables.
5+
Here is a list of environment variables that HTTPX recognizes
6+
and what function they serve:
7+
8+
`HTTPX_DEBUG`
9+
-----------
10+
11+
Valid values: `1`, `true`
12+
13+
If this environment variable is set to a valid value then low-level
14+
details about the execution of HTTP requests will be logged to `stderr`.
15+
16+
This can help you debug issues and see what's exactly being sent
17+
over the wire and to which location.
18+
19+
Example:
20+
21+
```python
22+
# test_script.py
23+
24+
import httpx
25+
client = httpx.Client()
26+
client.get("https://google.com")
27+
```
28+
29+
```console
30+
user@host:~$ HTTPX_DEBUG=1 python test_script.py
31+
20:54:17.585 - httpx.dispatch.connection_pool - acquire_connection origin=Origin(scheme='https' host='www.google.com' port=443)
32+
20:54:17.585 - httpx.dispatch.connection_pool - new_connection connection=HTTPConnection(origin=Origin(scheme='https' host='www.google.com' port=443))
33+
20:54:17.590 - httpx.dispatch.connection - start_connect host='www.google.com' port=443 timeout=TimeoutConfig(timeout=5.0)
34+
20:54:17.651 - httpx.dispatch.connection - connected http_version='HTTP/2'
35+
20:54:17.651 - httpx.dispatch.http2 - send_headers stream_id=1 headers=[(b':method', b'GET'), (b':authority', b'www.google.com'), ...]
36+
20:54:17.652 - httpx.dispatch.http2 - end_stream stream_id=1
37+
20:54:17.681 - httpx.dispatch.http2 - receive_event stream_id=0 event=<RemoteSettingsChanged changed_settings:{...}>
38+
20:54:17.681 - httpx.dispatch.http2 - receive_event stream_id=0 event=<WindowUpdated stream_id:0, delta:983041>
39+
20:54:17.682 - httpx.dispatch.http2 - receive_event stream_id=0 event=<SettingsAcknowledged changed_settings:{}>
40+
20:54:17.739 - httpx.dispatch.http2 - receive_event stream_id=1 event=<ResponseReceived stream_id:1, headers:[(b':status', b'200'), ...]>
41+
20:54:17.741 - httpx.dispatch.http2 - receive_event stream_id=1 event=<DataReceived stream_id:1, flow_controlled_length:5224 data:>
42+
20:54:17.742 - httpx.dispatch.http2 - receive_event stream_id=1 event=<DataReceived stream_id:1, flow_controlled_length:59, data:>
43+
20:54:17.742 - httpx.dispatch.http2 - receive_event stream_id=1 event=<StreamEnded stream_id:1>
44+
20:54:17.742 - httpx.dispatch.http2 - receive_event stream_id=0 event=<PingReceived ping_data:0000000000000000>
45+
20:54:17.743 - httpx.dispatch.connection_pool - release_connection connection=HTTPConnection(origin=Origin(scheme='https' host='www.google.com' port=443))
46+
```

httpx/dispatch/connection.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
VerifyTypes,
1616
)
1717
from ..models import AsyncRequest, AsyncResponse, Origin
18+
from ..utils import get_logger
1819
from .base import AsyncDispatcher
1920
from .http2 import HTTP2Connection
2021
from .http11 import HTTP11Connection
@@ -23,6 +24,9 @@
2324
ReleaseCallback = typing.Callable[["HTTPConnection"], typing.Awaitable[None]]
2425

2526

27+
logger = get_logger(__name__)
28+
29+
2630
class HTTPConnection(AsyncDispatcher):
2731
def __init__(
2832
self,
@@ -79,8 +83,10 @@ async def connect(
7983
else:
8084
on_release = functools.partial(self.release_func, self)
8185

86+
logger.debug(f"start_connect host={host!r} port={port!r} timeout={timeout!r}")
8287
stream = await self.backend.connect(host, port, ssl_context, timeout)
8388
http_version = stream.get_http_version()
89+
logger.debug(f"connected http_version={http_version!r}")
8490

8591
if http_version == "HTTP/2":
8692
self.h2_connection = HTTP2Connection(
@@ -102,6 +108,7 @@ async def get_ssl_context(self, ssl: SSLConfig) -> typing.Optional[ssl.SSLContex
102108
)
103109

104110
async def close(self) -> None:
111+
logger.debug("close_connection")
105112
if self.h2_connection is not None:
106113
await self.h2_connection.close()
107114
elif self.h11_connection is not None:
@@ -125,3 +132,7 @@ def is_connection_dropped(self) -> bool:
125132
else:
126133
assert self.h11_connection is not None
127134
return self.h11_connection.is_connection_dropped()
135+
136+
def __repr__(self) -> str:
137+
class_name = self.__class__.__name__
138+
return f"{class_name}(origin={self.origin!r})"

httpx/dispatch/connection_pool.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
VerifyTypes,
1313
)
1414
from ..models import AsyncRequest, AsyncResponse, Origin
15+
from ..utils import get_logger
1516
from .base import AsyncDispatcher
1617
from .connection import HTTPConnection
1718

1819
CONNECTIONS_DICT = typing.Dict[Origin, typing.List[HTTPConnection]]
1920

2021

22+
logger = get_logger(__name__)
23+
24+
2125
class ConnectionStore:
2226
"""
2327
We need to maintain collections of connections in a way that allows us to:
@@ -122,6 +126,7 @@ async def send(
122126
return response
123127

124128
async def acquire_connection(self, origin: Origin) -> HTTPConnection:
129+
logger.debug(f"acquire_connection origin={origin!r}")
125130
connection = self.active_connections.pop_by_origin(origin, http2_only=True)
126131
if connection is None:
127132
connection = self.keepalive_connections.pop_by_origin(origin)
@@ -141,12 +146,16 @@ async def acquire_connection(self, origin: Origin) -> HTTPConnection:
141146
backend=self.backend,
142147
release_func=self.release_connection,
143148
)
149+
logger.debug(f"new_connection connection={connection!r}")
150+
else:
151+
logger.debug(f"reuse_connection connection={connection!r}")
144152

145153
self.active_connections.add(connection)
146154

147155
return connection
148156

149157
async def release_connection(self, connection: HTTPConnection) -> None:
158+
logger.debug(f"release_connection connection={connection!r}")
150159
if connection.is_closed:
151160
self.active_connections.remove(connection)
152161
self.max_connections.release()

httpx/dispatch/http11.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ..concurrency.base import BaseStream, ConcurrencyBackend, TimeoutFlag
66
from ..config import TimeoutConfig, TimeoutTypes
77
from ..models import AsyncRequest, AsyncResponse
8+
from ..utils import get_logger
89

910
H11Event = typing.Union[
1011
h11.Request,
@@ -22,6 +23,9 @@
2223
OnReleaseCallback = typing.Callable[[], typing.Awaitable[None]]
2324

2425

26+
logger = get_logger(__name__)
27+
28+
2529
class HTTP11Connection:
2630
READ_NUM_BYTES = 4096
2731

@@ -61,6 +65,7 @@ async def send(
6165
async def close(self) -> None:
6266
event = h11.ConnectionClosed()
6367
try:
68+
logger.debug(f"send_event event={event!r}")
6469
self.h11_state.send(event)
6570
except h11.LocalProtocolError: # pragma: no cover
6671
# Premature client disconnect
@@ -73,6 +78,12 @@ async def _send_request(
7378
"""
7479
Send the request method, URL, and headers to the network.
7580
"""
81+
logger.debug(
82+
f"send_headers method={request.method!r} "
83+
f"target={request.url.full_path!r} "
84+
f"headers={request.headers!r}"
85+
)
86+
7687
method = request.method.encode("ascii")
7788
target = request.url.full_path.encode("ascii")
7889
headers = request.headers.raw
@@ -88,6 +99,7 @@ async def _send_request_data(
8899
try:
89100
# Send the request body.
90101
async for chunk in data:
102+
logger.debug(f"send_data data=Data(<{len(chunk)} bytes>)")
91103
event = h11.Data(data=chunk)
92104
await self._send_event(event, timeout)
93105

@@ -150,6 +162,12 @@ async def _receive_event(self, timeout: TimeoutConfig = None) -> H11Event:
150162
"""
151163
while True:
152164
event = self.h11_state.next_event()
165+
166+
if isinstance(event, h11.Data):
167+
logger.debug(f"receive_event event=Data(<{len(event.data)} bytes>)")
168+
else:
169+
logger.debug(f"receive_event event={event!r}")
170+
153171
if event is h11.NEED_DATA:
154172
try:
155173
data = await self.stream.read(
@@ -164,6 +182,11 @@ async def _receive_event(self, timeout: TimeoutConfig = None) -> H11Event:
164182
return event
165183

166184
async def response_closed(self) -> None:
185+
logger.debug(
186+
f"response_closed "
187+
f"our_state={self.h11_state.our_state!r} "
188+
f"their_state={self.h11_state.their_state}"
189+
)
167190
if (
168191
self.h11_state.our_state is h11.DONE
169192
and self.h11_state.their_state is h11.DONE

httpx/dispatch/http2.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from ..concurrency.base import BaseStream, ConcurrencyBackend, TimeoutFlag
88
from ..config import TimeoutConfig, TimeoutTypes
99
from ..models import AsyncRequest, AsyncResponse
10+
from ..utils import get_logger
11+
12+
logger = get_logger(__name__)
1013

1114

1215
class HTTP2Connection:
@@ -74,6 +77,15 @@ async def send_headers(
7477
(b":scheme", request.url.scheme.encode("ascii")),
7578
(b":path", request.url.full_path.encode("ascii")),
7679
] + [(k, v) for k, v in request.headers.raw if k != b"host"]
80+
81+
logger.debug(
82+
f"send_headers "
83+
f"stream_id={stream_id} "
84+
f"method={request.method!r} "
85+
f"target={request.url.full_path!r} "
86+
f"headers={headers!r}"
87+
)
88+
7789
self.h2_state.send_headers(stream_id, headers)
7890
data_to_send = self.h2_state.data_to_send()
7991
await self.stream.write(data_to_send, timeout)
@@ -100,11 +112,17 @@ async def send_data(
100112
chunk_size = min(len(data), flow_control)
101113
for idx in range(0, len(data), chunk_size):
102114
chunk = data[idx : idx + chunk_size]
115+
116+
logger.debug(
117+
f"send_data stream_id={stream_id} data=Data(<{len(chunk)} bytes>)"
118+
)
119+
103120
self.h2_state.send_data(stream_id, chunk)
104121
data_to_send = self.h2_state.data_to_send()
105122
await self.stream.write(data_to_send, timeout)
106123

107124
async def end_stream(self, stream_id: int, timeout: TimeoutConfig = None) -> None:
125+
logger.debug(f"end_stream stream_id={stream_id}")
108126
self.h2_state.end_stream(stream_id)
109127
data_to_send = self.h2_state.data_to_send()
110128
await self.stream.write(data_to_send, timeout)
@@ -151,7 +169,11 @@ async def receive_event(
151169
data = await self.stream.read(self.READ_NUM_BYTES, timeout, flag=flag)
152170
events = self.h2_state.receive_data(data)
153171
for event in events:
154-
if getattr(event, "stream_id", 0):
172+
event_stream_id = getattr(event, "stream_id", 0)
173+
logger.debug(
174+
f"receive_event stream_id={event_stream_id} event={event!r}"
175+
)
176+
if event_stream_id:
155177
self.events[event.stream_id].append(event)
156178

157179
data_to_send = self.h2_state.data_to_send()

httpx/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ def __eq__(self, other: typing.Any) -> bool:
243243
def __hash__(self) -> int:
244244
return hash((self.scheme, self.host, self.port))
245245

246+
def __repr__(self) -> str:
247+
class_name = self.__class__.__name__
248+
return (
249+
f"{class_name}(scheme={self.scheme!r} host={self.host!r} port={self.port})"
250+
)
251+
246252

247253
class QueryParams(typing.Mapping[str, str]):
248254
"""

httpx/utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import codecs
2+
import logging
23
import netrc
34
import os
45
import re
6+
import sys
57
import typing
68
from pathlib import Path
79

@@ -140,3 +142,30 @@ def parse_header_links(value: str) -> typing.List[typing.Dict[str, str]]:
140142
link[key.strip(replace_chars)] = value.strip(replace_chars)
141143
links.append(link)
142144
return links
145+
146+
147+
_LOGGER_INITIALIZED = False
148+
149+
150+
def get_logger(name: str) -> logging.Logger:
151+
"""Gets a `logging.Logger` instance and optionally
152+
sets up debug logging if the user requests it via
153+
the `HTTPX_DEBUG=1` environment variable.
154+
"""
155+
global _LOGGER_INITIALIZED
156+
157+
if not _LOGGER_INITIALIZED:
158+
_LOGGER_INITIALIZED = True
159+
if os.environ.get("HTTPX_DEBUG", "").lower() in ("1", "true"):
160+
logger = logging.getLogger("httpx")
161+
logger.setLevel(logging.DEBUG)
162+
handler = logging.StreamHandler(sys.stderr)
163+
handler.setFormatter(
164+
logging.Formatter(
165+
fmt="%(asctime)s.%(msecs)03d - %(name)s - %(message)s",
166+
datefmt="%H:%M:%S",
167+
)
168+
)
169+
logger.addHandler(handler)
170+
171+
return logging.getLogger(name)

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ nav:
1212
- Introduction: 'index.md'
1313
- QuickStart: 'quickstart.md'
1414
- Advanced Usage: 'advanced.md'
15+
- Environment Variables: 'environment_variables.md'
1516
- Parallel Requests: 'parallel.md'
1617
- Async Client: 'async.md'
1718
- Requests Compatibility: 'compatibility.md'

tests/test_utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import logging
12
import os
23

34
import pytest
45

6+
import httpx
7+
from httpx import utils
58
from httpx.utils import get_netrc_login, guess_json_utf, parse_header_links
69

710

@@ -87,3 +90,24 @@ def test_get_netrc_login():
8790
)
8891
def test_parse_header_links(value, expected):
8992
assert parse_header_links(value) == expected
93+
94+
95+
@pytest.mark.asyncio
96+
@pytest.mark.parametrize("httpx_debug", ["0", "1", "True", "False"])
97+
async def test_httpx_debug_enabled_stderr_logging(server, capsys, httpx_debug):
98+
os.environ["HTTPX_DEBUG"] = httpx_debug
99+
100+
# Force a reload on the logging handlers
101+
utils._LOGGER_INITIALIZED = False
102+
utils.get_logger("httpx")
103+
104+
async with httpx.AsyncClient() as client:
105+
await client.get("http://127.0.0.1:8000/")
106+
107+
if httpx_debug in ("1", "True"):
108+
assert "httpx.dispatch.connection_pool" in capsys.readouterr().err
109+
else:
110+
assert "httpx.dispatch.connection_pool" not in capsys.readouterr().err
111+
112+
# Reset the logger so we don't have verbose output in all unit tests
113+
logging.getLogger("httpx").handlers = []

0 commit comments

Comments
 (0)