-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add easier debug logging for users #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really excited about how helpful this will be for debugging requests! I left a couple of comments on details.
docs/environment_variables.md
Outdated
| Here are a list of environment variables that HTTPX recognizes | ||
| and what function they serve: | ||
|
|
||
| HTTPX_DEBUG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| HTTPX_DEBUG | |
| `HTTPX_DEBUG` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc: @tomchristie potential mkdocs-material improvement? :)
|
Also, will this fix #33? |
Co-Authored-By: Florimond Manca <[email protected]>
Co-Authored-By: Florimond Manca <[email protected]>
Co-Authored-By: Florimond Manca <[email protected]>
|
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple more polish suggestions!
|
@florimondmanca Incorporated your suggestions! |
florimondmanca
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably also want to log the sending of h11.Data and h11.EndOfMessage events, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
florimondmanca
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍👏
|
Thanks for the review @florimondmanca and @tomchristie! |

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.