logging.md
This file
Path
docs/logging.md
81 lines · 3.9 KiB · Markdown
Last changed by
[docs] Remove load_ssl_context & load_verify_locations DEBUG log (#3589)nikkiebefore adoption3fee278
History of this fileTake it
The walk stopped after 200 commits: lines older than 920333e point at it.
Always encode forward slashes as `%2F` in query parameters (#2723)Zanie Adkinsbefore adoption920333e
lines 1–22why, one step back1# Logging23If you need to inspect the internal behaviour of `httpx`, you can use Python's standard logging to output information about the underlying network behaviour.45For example, the following configuration...67```python8import logging9import httpx1011logging.basicConfig(12 format="%(levelname)s [%(asctime)s] %(name)s - %(message)s",13 datefmt="%Y-%m-%d %H:%M:%S",14 level=logging.DEBUG15)1617httpx.get("https://www.example.com")18```1920Will send debug level output to the console, or wherever `stdout` is directed too...2122```Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b
lines 23–39why, one step back23DEBUG [2024-09-28 17:27:40] httpcore.connection - connect_tcp.started host='www.example.com' port=443 local_address=None timeout=5.0 socket_options=None24DEBUG [2024-09-28 17:27:41] httpcore.connection - connect_tcp.complete return_value=<httpcore._backends.sync.SyncStream object at 0x101f1e8e0>25DEBUG [2024-09-28 17:27:41] httpcore.connection - start_tls.started ssl_context=SSLContext(verify=True) server_hostname='www.example.com' timeout=5.026DEBUG [2024-09-28 17:27:41] httpcore.connection - start_tls.complete return_value=<httpcore._backends.sync.SyncStream object at 0x1020f49a0>27DEBUG [2024-09-28 17:27:41] httpcore.http11 - send_request_headers.started request=<Request [b'GET']>28DEBUG [2024-09-28 17:27:41] httpcore.http11 - send_request_headers.complete29DEBUG [2024-09-28 17:27:41] httpcore.http11 - send_request_body.started request=<Request [b'GET']>30DEBUG [2024-09-28 17:27:41] httpcore.http11 - send_request_body.complete31DEBUG [2024-09-28 17:27:41] httpcore.http11 - receive_response_headers.started request=<Request [b'GET']>32DEBUG [2024-09-28 17:27:41] httpcore.http11 - receive_response_headers.complete return_value=(b'HTTP/1.1', 200, b'OK', [(b'Content-Encoding', b'gzip'), (b'Accept-Ranges', b'bytes'), (b'Age', b'407727'), (b'Cache-Control', b'max-age=604800'), (b'Content-Type', b'text/html; charset=UTF-8'), (b'Date', b'Sat, 28 Sep 2024 13:27:42 GMT'), (b'Etag', b'"3147526947+gzip"'), (b'Expires', b'Sat, 05 Oct 2024 13:27:42 GMT'), (b'Last-Modified', b'Thu, 17 Oct 2019 07:18:26 GMT'), (b'Server', b'ECAcc (dcd/7D43)'), (b'Vary', b'Accept-Encoding'), (b'X-Cache', b'HIT'), (b'Content-Length', b'648')])33INFO [2024-09-28 17:27:41] httpx - HTTP Request: GET https://www.example.com "HTTP/1.1 200 OK"34DEBUG [2024-09-28 17:27:41] httpcore.http11 - receive_response_body.started request=<Request [b'GET']>35DEBUG [2024-09-28 17:27:41] httpcore.http11 - receive_response_body.complete36DEBUG [2024-09-28 17:27:41] httpcore.http11 - response_closed.started37DEBUG [2024-09-28 17:27:41] httpcore.http11 - response_closed.complete38DEBUG [2024-09-28 17:27:41] httpcore.connection - close.started39DEBUG [2024-09-28 17:27:41] httpcore.connection - close.completeAlways encode forward slashes as `%2F` in query parameters (#2723)Zanie Adkinsbefore adoption920333e
lines 40–41why, one step back40```41fix a typo in docs/logging.md (#2830)xzmengbefore adoption053bc57
line 42why, one step back42Logging output includes information from both the high-level `httpx` logger, and the network-level `httpcore` logger, which can be configured separately.Always encode forward slashes as `%2F` in query parameters (#2723)Zanie Adkinsbefore adoption920333e
lines 43–81why, one step back4344For handling more complex logging configurations you might want to use the dictionary configuration style...4546```python47import logging.config48import httpx4950LOGGING_CONFIG = {51 "version": 1,52 "handlers": {53 "default": {54 "class": "logging.StreamHandler",55 "formatter": "http",56 "stream": "ext://sys.stderr"57 }58 },59 "formatters": {60 "http": {61 "format": "%(levelname)s [%(asctime)s] %(name)s - %(message)s",62 "datefmt": "%Y-%m-%d %H:%M:%S",63 }64 },65 'loggers': {66 'httpx': {67 'handlers': ['default'],68 'level': 'DEBUG',69 },70 'httpcore': {71 'handlers': ['default'],72 'level': 'DEBUG',73 },74 }75}7677logging.config.dictConfig(LOGGING_CONFIG)78httpx.get('https://www.example.com')79```8081The exact formatting of the debug logging may be subject to change across different versions of `httpx` and `httpcore`. If you need to rely on a particular format it is recommended that you pin installation of these packages to fixed versions.