DEV Community

WDSEGA
WDSEGA

Posted on • Originally published at wdsega.github.io

Python Logging in 2026: 10 Practices That Actually Matter for Production

Production logging is where most Python applications have their worst technical debt. The docs are thorough. The tutorials are everywhere. And yet logs are still useless when you actually need them.

Here are 10 practices that make logs useful before an incident forces you to find out.

1. Use Structured Logging From Day One

Plain text logs are for humans. Structured logs are for humans AND every aggregation tool.

import structlog
log = structlog.get_logger()
log.info("payment_processed", user_id=user.id, amount=order.total, order_id=order.id)
Enter fullscreen mode Exit fullscreen mode

2. Set Log Levels That Mean Something

  • DEBUG: verbose internals, off in production
  • INFO: normal events worth recording
  • WARNING: unexpected but recovered from
  • ERROR: something failed that should not have
  • CRITICAL: application cannot continue

3. Propagate Request Context Automatically

Use contextvars to bind request ID, user ID automatically to all log entries.

4. Never Log Sensitive Data

Passwords, API keys, PII. Audit log output in staging. Scan for password, token, secret, ssn.

5. Use Separate Handlers for Different Destinations

Console for development, file for persistence, error-only file for alerting.

6. Include Timing in Slow Operations

start = time.monotonic()
result = expensive_operation()
elapsed = time.monotonic() - start
log.info("query_complete", duration_ms=round(elapsed * 1000, 2))
Enter fullscreen mode Exit fullscreen mode

7. Manage Third-Party Library Logs

logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
Enter fullscreen mode Exit fullscreen mode

8. Log at Exception Boundaries

Every bare except block that swallows exceptions silently is a future debugging nightmare. Use exc_info=True to capture the full stack trace.

9. Rotate Log Files

Use RotatingFileHandler with maxBytes and backupCount. Five minutes of setup prevents a disk-full outage.

10. Test Your Logging Configuration

Verify production log level, check for sensitive data leakage, confirm logs reach your aggregation system. Quarterly audit.


For teams needing deeper analysis - pattern detection, anomaly flagging, incident reconstruction - LogAnalysis Pro bundles those tools into one offline Python package.

Top comments (0)