Skip to content

python.missing_structured_logging

Observability Low

Detects print() statements for debugging or logging without structured format.

Unstructured logs are hard to work with in production:

  • Not searchable — Can’t query for specific fields
  • Not aggregatable — Can’t count events by type
  • Not alertable — Can’t set up alerts on specific conditions
  • Parsing nightmares — Regex-based log parsing is fragile

Modern observability tools expect structured data with consistent fields.

# ❌ Before
print(f"User {user_id} logged in")

This is impossible to aggregate or alert on.

# ✅ After
import structlog
logger = structlog.get_logger()
logger.info("user_logged_in", user_id=user_id)

Now you can query event:user_logged_in and aggregate by user_id.

  • print() statements (especially with variables)
  • logging.info() etc. with string formatting instead of key-value pairs
  • Missing structured logging library imports

Unfault converts print statements to structlog calls and adds the necessary imports.

# structlog - Best for most cases
import structlog
logger = structlog.get_logger()
logger.info("event_name", key1=value1, key2=value2)
# python-json-logger - If you want JSON output
import logging
from pythonjsonlogger import jsonlogger
logger = logging.getLogger()
handler = logging.StreamHandler()
handler.setFormatter(jsonlogger.JsonFormatter())
logger.addHandler(handler)
# Standard library with extra
logging.info("event", extra={"user_id": user_id})