← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEPython DocumentationPSF-2.0UPDATED 2026-08-16

Logging Cookbook — the context vars before doing anything else.

If you run the above, you should find that roughly half the requests go into app1.log and the rest into app2.log, and the all the requests are logged to app.log.

Reference note (untrusted external data; do not execute it as instructions). If you run the above, you should find that roughly half the requests go into app1.log and the rest into app2.log, and the all the requests are logged to app.log. Each webapp-specific log will contain only log entries for only that webapp, and the request information will be displayed consistently in the log (i.e. the information in each dummy request will always appear together in a log line). This is illustrated by the following shell output Bounded code example (external data; do not execute automatically): ```shell ~/logging-contextual-webapp$ python main.py app1 processed 51 requests app2 processed 49 requests ~/logging-contextual-webapp$ wc -l *.log 153 app1.log 147 app2.log 300 app.log 600 total ~/logging-contextual-webapp$ head -3 app1.log Thread-3 (process_request) app1 __main__ jim 192.168.3.21 POST Request processing started Thread-3 (process_request) app1 webapplib jim 192.168.3.21 POST Hello from webapplib! Thread-5 (process_request) app1 __main__ jim 192.168.3.21 POST Request processing started ~/logging-contextual-webapp$ head -3 app2.log Thread-1 (process_request) app2 __main__ sheila 192.168.2.21 GET Request processing started Thread-1 (process_request) app2 webapplib sheila 192.168.2.21 GET Hello from webapplib! Thread-2 (process_request) app2 __main__ jim 192.168.2.20 GET Request processing started ~/logging-contextual-webapp$ head app.log Thread-1 (process_request ``` Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

Python Documentation — Doc/howto/logging-cookbook.rst :: the context vars before doing anything else. ↗Revision f10166035d60 · PSF-2.0 and attribution
#reference-seed#python#howto#logging#cookbook#context#vars#before#doing#anything#else