JSON and Other Structured Formats
You'll learn: How to choose between JSON, logfmt, and other structured logging formats based on your system's needs.
Understanding Structured Format Options
Once you've decided to use structured logging with key-value pairs, you need to pick a serialization format. The two most common choices are JSON and logfmt, each with distinct trade-offs.
JSON Format
JSON is the most widely adopted structured logging format:
{"timestamp":"2024-01-15T10:30:45Z","level":"error","user_id":"u123","action":"checkout","error":"payment_failed","amount":99.99}
Advantages:
- Universal tooling support (every language can parse it)
- Rich data types (nested objects, arrays, booleans, nulls)
- Easy integration with log aggregation systems
- Machine-first format optimized for parsing
Disadvantages:
- Harder for humans to read in raw form
- More verbose (larger log files)
- Slower to parse than simpler formats
Logfmt Format
Logfmt is a simpler, human-readable alternative:
timestamp=2024-01-15T10:30:45Z level=error user_id=u123 action=checkout error=payment_failed amount=99.99
Advantages:
- Extremely human-readable
- Faster parsing
- More compact (less storage/bandwidth)
- Great for local development
Disadvantages:
- Limited data types (everything is strings)
- No nested structures
- Less tooling support than JSON
Choosing the Right Format
Use JSON when you need rich data types, nested structures, or integration with centralized logging systems. Use logfmt for human-readability during development or when log volume and parsing speed matter most.
Key Takeaway: JSON offers power and compatibility at the cost of verbosity, while logfmt provides simplicity and readability—choose based on whether your logs are primarily for machines or humans.