Home / Learn Secure SQL / Structured Logging

Structured Logging

Logging as data (JSON/key-value), not just free-text sentences.

The gistLogging as data (JSON/key-value), not just free-text sentences.

The idea, explained simply

Free-text vs structuredFree-text"Error updating user 42 at10:03"Hard to search/filter reliablyStructured (JSON){"level":"error","user_id":42,"action":"update","ts":"..."}Searchable and filterableprecisely
Free-text vs structured

The basic idea

Structured logging writes each log entry as a consistent set of fields (timestamp, level, message, correlation ID, user ID) usually as JSON, rather than a free-form sentence, so logs can be searched, filtered, and alerted on reliably.

Why this comes up

Free-text logs ("Error updating user 42 at 10:03") are hard to search reliably at scale; structured fields let you query "every error for user_id=42" precisely, across millions of log lines.

The mechanics

Use a structured logging library for your language (structlog in Python, Winston or Pino in Node, Logback with a JSON encoder in Java), and log consistent field names across your whole codebase.

Where it is used

  • Web application code review
  • Backend development
  • Database administration
Structured log entry (Python structlog)
import structlog
log = structlog.get_logger()
log.error("order_update_failed", order_id=order_id, reason=str(e))

Quick check

1. A benefit of structured (JSON) logging over free-text is...