Reference library

Modern tooling

uv, ruff, pyproject.toml, packaging, and current Python project workflows.

2 matches
Modern tooling medium

How to Bind and Mock structlog Context in Python

Shows how to bind persistent key-value context to a structlog logger, unbind keys, and mock the logger in tests to verify context is passed correctly.

structlog logging mocking
Python
import structlog
from unittest.mock import patch

logger = structlog.get_logger()

def demo():
    logger = structlog.get_logger()
    logger = logger.bind(user_id=42, request_id="abc123")
    logger.info("user logged in", action="login")
    
    # Unbind a key
    logger = logger.unbind("user_id")
    logger.info("r…
17 0 Open
Modern tooling medium

Mocking loguru for Structured Logging in Python

Simulate loguru's structured logging with a custom mock that captures JSON-formatted log entries with bound context.

loguru logging mock
Python
import json
import sys
from io import StringIO
from unittest.mock import patch


def mock_loguru():
    # Simulate a structured logger with context binding
    class StructuredLogger:
        def __init__(self):
            self.context = {}

        def bind(self, **kwargs):
            logger = StructuredLogger()
  …
12 0 Open

Browse by section

Each section groups closely related Python snippets.

Modern tooling — Python code examples

What you will find here

This page collects modern tooling snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.