Structlog Contextual Bound Logging
Bind request-scoped context with structlog, so every log line carries correlation fields without threading state through call stacks.
Code
import logging
import uuid
import structlog
structlog.configure(
processors=[
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt='iso'),
structlog.processors.JSONRenderer(),
],
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
logger_factory=structlog.PrintLoggerFactory(),
)
logger = structlog.get_logger()
def charge_order(order_id: str, amount: int) -> None:
logger.info('charge_started', order_id=order_id, amount=amount)
try:
if amount > 100_000:
raise ValueError('amount exceeds single-charge limit')
except ValueError:
logger.exception('charge_rejected', order_id=order_id)
raise
def handle_request(request_id: str, order: dict) -> None:
structlog.contextvars.bind_contextvars(
request_id=request_id,
tenant=order['tenant'],
)
charge_order(order['id'], order['amount'])
handle_request(
request_id=str(uuid.uuid4())[:8],
order={'id': 'ord-91', 'tenant': 'acme', 'amount': 250_000},
)What the code does
The working surface is the functions charge_order and handle_request — everything else is wiring.
ValueError and def appears at a raise inside an except handler — it fires only while that failure type is already being handled.
What to watch for
- Requires
loggingandstructlogas packages; the snippet will not run without them. - The guard
amount > 100_000short-circuits the flow with an earlyraisewhen its condition holds.
Applying it
The primary signature is def charge_order(order_id: str, amount: int) -> None
charge_order is what another module would consume; nothing else here is load-bearing for callers.
Statements that matter
def handle_request(request_id: str, order: dict) -> None:
structlog.contextvars.bind_contextvars(
request_id=request_id,
tenant=order['tenant'],
)
charge_order(order['id'], order['amount'])
except ValueError:
logger.exception('charge_rejected', order_id=order_id)
raise
Interactive Tools for This Topic
FAQ
- When is the exception raised?
- Inside an `except` handler — the `ValueError` and `def` fires only while that failure type is already being handled, not on its own trigger.
- What does this need from the package registry?
- These are real dependencies: `logging` and `structlog`. Without them the snippet will not load.
Report a problem with this exampleOpens a pre-filled issue on GitHub.