logging.md
This file
Path
docs/logging.md
83 lines · 4.1 KiB · Markdown
Last changed by
fix a typo in docs/logging.md (#2830)xzmengbefore adoption053bc57
History of this fileTake it
Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 1–43why, 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```23DEBUG [2023-03-16 14:36:20] httpx - load_ssl_context verify=True cert=None trust_env=True http2=False24DEBUG [2023-03-16 14:36:20] httpx - load_verify_locations cafile='/Users/tomchristie/GitHub/encode/httpx/venv/lib/python3.10/site-packages/certifi/cacert.pem'25DEBUG [2023-03-16 14:36:20] httpcore - connection.connect_tcp.started host='www.example.com' port=443 local_address=None timeout=5.026DEBUG [2023-03-16 14:36:20] httpcore - connection.connect_tcp.complete return_value=<httpcore.backends.sync.SyncStream object at 0x1068fd270>27DEBUG [2023-03-16 14:36:20] httpcore - connection.start_tls.started ssl_context=<ssl.SSLContext object at 0x10689aa40> server_hostname='www.example.com' timeout=5.028DEBUG [2023-03-16 14:36:20] httpcore - connection.start_tls.complete return_value=<httpcore.backends.sync.SyncStream object at 0x1068fd240>29DEBUG [2023-03-16 14:36:20] httpcore - http11.send_request_headers.started request=<Request [b'GET']>30DEBUG [2023-03-16 14:36:20] httpcore - http11.send_request_headers.complete31DEBUG [2023-03-16 14:36:20] httpcore - http11.send_request_body.started request=<Request [b'GET']>32DEBUG [2023-03-16 14:36:20] httpcore - http11.send_request_body.complete33DEBUG [2023-03-16 14:36:20] httpcore - http11.receive_response_headers.started request=<Request [b'GET']>34DEBUG [2023-03-16 14:36:21] 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'507675'), (b'Cache-Control', b'max-age=604800'), (b'Content-Type', b'text/html; charset=UTF-8'), (b'Date', b'Thu, 16 Mar 2023 14:36:21 GMT'), (b'Etag', b'"3147526947+ident"'), (b'Expires', b'Thu, 23 Mar 2023 14:36:21 GMT'), (b'Last-Modified', b'Thu, 17 Oct 2019 07:18:26 GMT'), (b'Server', b'ECS (nyb/1D2E)'), (b'Vary', b'Accept-Encoding'), (b'X-Cache', b'HIT'), (b'Content-Length', b'648')])35INFO [2023-03-16 14:36:21] httpx - HTTP Request: GET https://www.example.com "HTTP/1.1 200 OK"36DEBUG [2023-03-16 14:36:21] httpcore - http11.receive_response_body.started request=<Request [b'GET']>37DEBUG [2023-03-16 14:36:21] httpcore - http11.receive_response_body.complete38DEBUG [2023-03-16 14:36:21] httpcore - http11.response_closed.started39DEBUG [2023-03-16 14:36:21] httpcore - http11.response_closed.complete40DEBUG [2023-03-16 14:36:21] httpcore - connection.close.started41DEBUG [2023-03-16 14:36:21] httpcore - connection.close.complete42```43fix a typo in docs/logging.md (#2830)xzmengbefore adoption053bc57
line 44why, one step back44Logging output includes information from both the high-level `httpx` logger, and the network-level `httpcore` logger, which can be configured separately.Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 45–83why, one step back4546For 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.