Skip to content
Myr

logging.md

This file

Path

docs/logging.md

83 lines · 4.1 KiB · Markdown

Last changed by

Introduce new `SSLContext` API & escalate deprecations. (#3319)Tom Christiebefore adoption8e36f2b

History of this file

Take it

The walk stopped after 200 commits: lines older than 21e8a7d point at it.

Bump mkdocs-material from 9.0.15 to 9.1.5 (#2640)dependabot[bot]before adoption21e8a7d

lines 1–22why, one step back
1# Logging
23If 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–41why, one step back
23DEBUG [2024-09-28 17:27:40] httpx - load_ssl_context verify=True cert=None
24DEBUG [2024-09-28 17:27:40] httpx - load_verify_locations cafile='/Users/karenpetrosyan/oss/karhttpx/.venv/lib/python3.9/site-packages/certifi/cacert.pem'25DEBUG [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=None26DEBUG [2024-09-28 17:27:41] httpcore.connection - connect_tcp.complete return_value=<httpcore._backends.sync.SyncStream object at 0x101f1e8e0>27DEBUG [2024-09-28 17:27:41] httpcore.connection - start_tls.started ssl_context=SSLContext(verify=True) server_hostname='www.example.com' timeout=5.028DEBUG [2024-09-28 17:27:41] httpcore.connection - start_tls.complete return_value=<httpcore._backends.sync.SyncStream object at 0x1020f49a0>29DEBUG [2024-09-28 17:27:41] httpcore.http11 - send_request_headers.started request=<Request [b'GET']>30DEBUG [2024-09-28 17:27:41] httpcore.http11 - send_request_headers.complete31DEBUG [2024-09-28 17:27:41] httpcore.http11 - send_request_body.started request=<Request [b'GET']>32DEBUG [2024-09-28 17:27:41] httpcore.http11 - send_request_body.complete33DEBUG [2024-09-28 17:27:41] httpcore.http11 - receive_response_headers.started request=<Request [b'GET']>34DEBUG [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')])35INFO [2024-09-28 17:27:41] httpx - HTTP Request: GET https://www.example.com "HTTP/1.1 200 OK"36DEBUG [2024-09-28 17:27:41] httpcore.http11 - receive_response_body.started request=<Request [b'GET']>37DEBUG [2024-09-28 17:27:41] httpcore.http11 - receive_response_body.complete38DEBUG [2024-09-28 17:27:41] httpcore.http11 - response_closed.started39DEBUG [2024-09-28 17:27:41] httpcore.http11 - response_closed.complete40DEBUG [2024-09-28 17:27:41] httpcore.connection - close.started41DEBUG [2024-09-28 17:27:41] httpcore.connection - close.complete

Bump mkdocs-material from 9.0.15 to 9.1.5 (#2640)dependabot[bot]before adoption21e8a7d

lines 42–43why, one step back
42```
43

fix a typo in docs/logging.md (#2830)xzmengbefore adoption053bc57

line 44why, one step back
44Logging output includes information from both the high-level `httpx` logger, and the network-level `httpcore` logger, which can be configured separately.

Bump mkdocs-material from 9.0.15 to 9.1.5 (#2640)dependabot[bot]before adoption21e8a7d

lines 45–83why, one step back
45
46For handling more complex logging configurations you might want to use the dictionary configuration style...4748```python49import logging.config50import httpx5152LOGGING_CONFIG = {53 "version": 1,54 "handlers": {55 "default": {56 "class": "logging.StreamHandler",57 "formatter": "http",58 "stream": "ext://sys.stderr"59 }60 },61 "formatters": {62 "http": {63 "format": "%(levelname)s [%(asctime)s] %(name)s - %(message)s",64 "datefmt": "%Y-%m-%d %H:%M:%S",65 }66 },67 'loggers': {68 'httpx': {69 'handlers': ['default'],70 'level': 'DEBUG',71 },72 'httpcore': {73 'handlers': ['default'],74 'level': 'DEBUG',75 },76 }77}7879logging.config.dictConfig(LOGGING_CONFIG)80httpx.get('https://www.example.com')81```8283The 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.