Repository navigation
Add easier debug logging for users - #277
Conversation
|
This is a great idea. I'll take a closer look later, but do you know about structlog? Your |
|
I initially thought to use structlog, mostly didn't want to add a dependency just so we can do debug logging on a client library. Are there other structured logging libraries out there? |
|
I don't know of other structured logging libraries. But I think the real question here is, as you said: is adding another dependency for debug logs worth it? I'm not sure it is either. :-) It's probably possible to build our own formatting helper, starting with something really simple. There's a structured logging section in the Logging cookbook, which could helper us build a helper to be used like this: from httpx.logging import structured
logger.debug(structured("log message", key="value"))The cookbook recipe dumps the output as JSON, but we can take inspiration from structlog's rendering implementation here too to achieve the style you've got here. |
|
I think all of this can be figured out later as a refactoring step for this PR, though — just jotting down ideas here, but it's not a blocker at all. :-) |
florimondmanca
left a comment
There was a problem hiding this comment.
Really excited about how helpful this will be for debugging requests! I left a couple of comments on details.
| Here are a list of environment variables that HTTPX recognizes | ||
| and what function they serve: | ||
|
|
||
| HTTPX_DEBUG |
There was a problem hiding this comment.
| HTTPX_DEBUG | |
| `HTTPX_DEBUG` |
There was a problem hiding this comment.
Doesn't make any display difference using mkdocs-material (I tried it) but we'll go with it anyways :)
There was a problem hiding this comment.
Or really? I looked at the "Developer Interface" page and e.g. Client is rendered in a monospace font. Perhaps it's the use of === / --- instead of # and ## for headers?
There was a problem hiding this comment.
cc: @tomchristie potential mkdocs-material improvement? :)
|
Also, will this fix #33? |
Co-Authored-By: Florimond Manca <florimond.manca@gmail.com>
Co-Authored-By: Florimond Manca <florimond.manca@gmail.com>
Co-Authored-By: Florimond Manca <florimond.manca@gmail.com>
|
Not quite, that issue is way too broad for its own good. Maybe we should close it and open issues with the specific tasks it requires. |
florimondmanca
left a comment
There was a problem hiding this comment.
A couple more polish suggestions!
|
@florimondmanca Incorporated your suggestions! |
florimondmanca
left a comment
There was a problem hiding this comment.
One last suggestion I didn’t see earlier and we’ll be good to merge 🚀
| f"send_request method={request.method!r} " | ||
| f"target={request.url.full_path!r} " | ||
| f"headers={request.headers!r}" | ||
| ) |
There was a problem hiding this comment.
We probably also want to log the sending of h11.Data and h11.EndOfMessage events, right?
There was a problem hiding this comment.
Yeah I can add that!
|
Fantastic, I'm loving this! Couple of comments worth taking a look at. |
|
Possible later additions here:
INFO httpx.client GET https://example.com/redirect-to-home 304 <Location: https://example.com/>
INFO httpx.client GET https://example.com/ 200
WARN http.client GET https://example.com/ Failed with ReadTimeout(...) |
|
With the latest change I tried to normalize the two sets of events being logged for HTTP/1.1 and HTTP/2 so they're similar to look at and debug. HTTP/1.1 HTTP/2 |
|
Thanks for the review @florimondmanca and @tomchristie! |
* Ensured AnyIO 3.x compatibility * Fixed linting errors * Improved the comment * Fixed deprecation warnings * Fixed mypy errors and upgraded AnyIO

This builds on #240 and adds it to all request-making dispatchers. It can be added to ASGI and WSGI dispatchers as well but I'm less familiar with what information would be useful for debugging those use-cases.
This also adds a new environment variable
HTTPX_DEBUGwhich can be turned on to automatically display these debug logs.A new section
Environment Variableshas been added to the documentation with the intent of future expansion for all environment variables that HTTPX will react to.Closes #221.