Structured Logging with NDJSON

Why Newline-Delimited JSON (NDJSON) outperforms unstructured log lines for local debugging and log aggregation.

Systems Practical 5 min read Nov 27, 2026

Unstructured plain-text logs require fragile regular expressions to query. Newline-Delimited JSON (NDJSON) writes one self-contained JSON object per line, making logs immediately queryable with jq, DuckDB, or any log ingestion pipeline.

Core Schema Fields

Standardize a minimal envelope across every service and CLI tool:

{"ts":"2026-11-28T15:04:05.120Z","level":"info","event":"ingest.complete","records":1420,"duration_ms":84}
{"ts":"2026-11-28T15:04:05.215Z","level":"warn","event":"schema.fallback","field":"altitude_m","reason":"missing_unit"}
  • ts: ISO-8601 UTC timestamp with millisecond precision.
  • level: Normalized severity (debug, info, warn, error).
  • event: Dot-namespaced stable event identifier (domain.action) instead of dynamic prose strings.

Querying Locally with DuckDB

Because NDJSON is valid line-oriented JSON, you can run SQL queries directly against local log files without importing them first:

SELECT event, count(*) AS total, avg(duration_ms) AS avg_ms
FROM read_json_auto('logs/*.ndjson')
WHERE level = 'info'
GROUP BY event
ORDER BY total DESC;
#observability #logging #json