← ClaudeAtlas

logging-patternslisted

Python logging patterns covering stdlib logging configuration, structured logging with structlog, log levels, handlers, formatters, and context binding. Use whenever a project uses Python logging, structlog, or the user asks about log levels, log formatting, structured logs, or JSON logging, even if "logging" is not mentioned by name.
ku5ic/dotfiles · ★ 0 · Data & Documents · score 72
Install: claude install-skill ku5ic/dotfiles
# Logging Patterns Review checklist plus the configuration shapes that are easy to get wrong. Level semantics and handler constructors are in the stdlib docs. ## stdlib gotchas - `basicConfig` is a no-op if the root logger already has handlers. Call it once at the entry point; pass `force=True` to replace existing handlers (useful in tests). - Records propagate up the dot-separated hierarchy to parent handlers. Set `propagate = False` only when you have deliberately attached a handler to that logger - otherwise you get every record twice. - Silence a noisy dependency by name rather than lowering the global level: `logging.getLogger("httpx").setLevel(logging.WARNING)`. ## structlog configuration The processor chain is order-sensitive and the API shifts between minor versions - pin this shape and verify against the docs on upgrade. ```python import logging import structlog structlog.configure( processors=[ structlog.contextvars.merge_contextvars, structlog.processors.add_log_level, structlog.processors.StackInfoRenderer(), structlog.dev.set_exc_info, structlog.processors.TimeStamper(fmt="iso", utc=True), structlog.dev.ConsoleRenderer(), # JSONRenderer() in production ], wrapper_class=structlog.make_filtering_bound_logger(logging.DEBUG), logger_factory=structlog.PrintLoggerFactory(), cache_logger_on_first_use=True, ) ``` - Request-scoped context that survives awaits and task boundaries goes through `str