structlog-python
БесплатноБез исполняемых скриптовНе проверенAdd structured logging to Python with structlog: key-value events rendered as JSON in production or colored console lines in development, request-scoped…
от TerminalSkills★ 221 звезда на GitHubОб этом скилле
structlog
Overview
structlog adds structured, context-rich logging to Python. Instead of format strings, you pass key-value pairs that render as JSON (production) or colored console lines (development). Bound loggers and contextvars carry context such as a request ID across function calls, and the standard-library integration routes logs from other libraries through the same pipeline. Install with pip install structlog; release 26.1 needs Python 3.10 or newer (3.8 and 3.9 support was removed).
Instructions
Step 1: Configuration
Call this once at startup. Logs from structlog and from plain logging loggers (database drivers, SDKs) end up in the same renderer: foreign_pre_chain runs the shared processors on standard-library records, and remove_processors_meta strips structlog's internal keys before rendering.
# logging_config.py: structlog setup
import logging
import sys
import structlog
def setup_logging(environment: str = "development", level: int = logging.INFO) -> None:
"""'development' prints colored console lines, anything else prints JSON."""
shared_processors = [
structlog.contextvars.merge_contextvars,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.processors.TimeStamper(fmt="iso", utc=True),
structlog.processors.StackInfoRenderer(),
]
if environment == "development":
renderer = structlog.dev.ConsoleRenderer()
else:
renderer = structlog.processors.JSONRenderer()
structlog.configure(
processors=[
*shared_processors,
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(structlog.stdlib.ProcessorFormatter(
foreign_pre_chain=shared_processors,
processors=[
structlog.stdlib.ProcessorFormatter.remove_processors_meta,
structlog.processors.format_exc_info, # exceptions as a string field in JSON
renderer,
],
))
root = logging.getLogger()
root.handlers.clear() # safe to call twice, no duplicated lines
root.addHandler(handler)
root.setLevel(level)
Step 2: Usage
# services/orders.py: structured logging in business logic
import structlog
log = structlog.get_logger(__name__)
async def process_order(order_id: str, user_id: str):
# bind() returns a new logger carrying this context
order_log = log.bind(order_id=order_id, user_id=user_id)
order_log.info("order_processing_started")
items = await fetch_order_items(order_id)
order_log.info("items_fetched", item_count=len(items), total=sum(i.price for i in items))
try:
payment = await charge_payment(order_id)
order_log.info("payment_charged", payment_id=payment.id, amount=payment.amount)
except PaymentError as exc:
order_log.exception("payment_failed", error_code=exc.code) # includes the traceback
raise
order_log.info("order_completed", status="success")
Step 3: Request context (FastAPI)
bind_contextvars stores values per request task; merge_contextvars in Step 1 adds them to every event, including those from standard-library loggers.
# main.py: add request context to all logs
from uuid import uuid4
import structlog
from fastapi import FastAPI, Request
from logging_config import setup_logging
setup_logging("production")
log = structlog.get_logger(__name__)
app = FastAPI()
@app.middleware("http")
async def logging_middleware(request: Request, call_next):
request_id = request.headers.get("x-request-id", str(uuid4()))
structlog.contextvars.clear_contextvars()
structlog.contextvars.bind_contextvars(
request_id=request_id,
method=request.method,
path=request.url.path,
)
log.info("request_started")
response = await call_next(request)
log.info("request_completed", status_code=response.status_code)
response.headers["x-request-id"] = request_id
return response
Examples
Example 1: "Make our logs JSON with a request ID"
After Steps 1 and 3, a route handler for GET /orders/{order_id} that calls log.info("order_fetched", order_id=order_id, item_count=3) and a request with header x-request-id: req-77 print one JSON object per line (plus a request_started line first, omitted here):
{"order_id": "A-1001", "item_count": 3, "event": "order_fetched", "request_id": "req-77", "path": "/orders/A-1001", "method": "GET", "logger": "services.orders", "level": "info", "timestamp": "2026-10-24T19:18:26.776495Z"}
{"status_code": 200, "event": "request_completed", "request_id": "req-77", "path": "/orders/A-1001", "method": "GET", "logger": "main", "level": "info", "timestamp": "
Установить structlog-python в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл structlog-python?
Add structured logging to Python with structlog: key-value events rendered as JSON in production or colored console lines in development, request-scoped context through contextvars, and standard-library integration so third-party library logs share the same format. Use when a user asks to implement structured logging, add request IDs or context to Python logs, configure log processors, or replace print and logging format strings in a FastAPI or Flask service.
Как установить скилл structlog-python?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл structlog-python запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
от Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
от Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropic