executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
structlogstructlog - structured logging library for Python with native JSON support, context binding, and processor pipeline. Integrates with FastAPI, Django, and standard logging module. USE WHEN: user mentions "structlog", "python structured logging", "context binding", asks about "JSON logging python", "fastapi logging", "django structured logging" DO NOT USE F…
Independent GitHub snapshot · Automated classification · Original documentation governs
name: structlog description: | structlog - structured logging library for Python with native JSON support, context binding, and processor pipeline. Integrates with FastAPI, Django, and standard logging module.
USE WHEN: user mentions "structlog", "python structured logging", "context binding", asks about "JSON logging python", "fastapi logging", "django structured logging"
python-logging instead, Node.js logging - use pino or winston,
Java logging - use slf4j or logback instead
allowed-tools: Read, Grep, Glob, Write, EditDeep Knowledge: Use
mcp__documentation__fetch_docswith technology:structlogfor comprehensive documentation.
pip install structlog
import structlog
structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.processors.JSONRenderer()
],
wrapper_class=structlog.stdlib.BoundLogger,
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True,
)
import structlog
log = structlog.get_logger()
log.info("user_logged_in", user_id=123, ip="192.168.1.1")
log.warning("rate_limit_exceeded", endpoint="/api/users", count=100)
log.error("database_error", error="connection timeout", retry=3)
log = structlog.get_logger()
# Bind context for all subsequent logs
log = log.bind(request_id="abc-123", user_id=42)
log.info("processing_started") # Includes request_id and user_id
log.info("step_completed", step=1)
log.info("processing_finished")
# New context
log = log.new(request_id="xyz-789")
from fastapi import FastAPI, Request
import structlog
app = FastAPI()
@app.middleware("http")
async def add_request_context(request: Request, call_next):
structlog.contextvars.clear_contextvars()
structlog.contextvars.bind_contextvars(
request_id=request.headers.get("X-Request-ID", str(uuid.uuid4())),
path=request.url.path,
)
return await call_next(request)
# settings.py
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"json": {
"()": structlog.stdlib.ProcessorFormatter,
"processor": structlog.processors.JSONRenderer(),
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "json",
},
},
"root": {
"handlers": ["console"],
"level": "INFO",
},
}
try:
risky_operation()
except Exception:
log.exception("operation_failed", operation="risky")
# Automatically includes stack trace
| Anti-Pattern | Why It's Bad | Solution |
|---|---|---|
| Using ConsoleRenderer in production | Wastes CPU, not machine-parseable | Use JSONRenderer for production |
| Not clearing context variables | Leaks context across requests | Use structlog.contextvars.clear_contextvars() |
| Logging large objects | Serialization overhead | Log only necessary fields or IDs |
| Creating new logger per request | Performance overhead | Use logger.bind() to add context |
| Missing exception logging | Loses stack traces | Use logger.exception() in except blocks |
| Not configuring processors | Incomplete/inconsistent output | Configure full processor pipeline |
| Issue | Cause | Solution |
|---|---|---|
| Plain text output instead of JSON | ConsoleRenderer configured | Change to JSONRenderer() in processors |
| Context not appearing in logs | Not using context binding | Use logger.bind() or contextvars |
| Performance issues | Too many processors | Remove unnecessary processors, use JSONRenderer |
| Missing timestamps | No TimeStamper processor | Add TimeStamper(fmt='iso') to processors |
| Logs not colorized in dev | Missing dev configuration | Use ConsoleRenderer(colors=True) for development |
| Context bleeding across requests | Not clearing contextvars | Clear context at request start with middleware |