Definition:
Structured Logging is a practice of writing log messages in a structured, machine-readable format (usually JSON) instead of plain text strings. Instead of embedding data within human-readable sentences, structured logs separate the message from the contextual data, making logs easily searchable, filterable, and analyzable by automated tools and log management systems.
Key characteristics:
- Uses structured formats (JSON, key-value pairs)
- Separates message from contextual data
- Machine-readable and parseable
- Enables powerful log analysis and searching
- Essential for microservices and distributed systems
- Improves observability and debugging
Example:
E-commerce Order Processing Scenario: Logging when a customer places an order
Structured Logging (Easy to Parse):
┌─────────────────────────────────────────────────────┐
│ STRUCTURED LOGS (Machine-Friendly) │
└─────────────────────────────────────────────────────┘
{
"timestamp": "2024-01-15T10:30:45Z",
"level": "INFO",
"message": "Order placed successfully",
"user_id": "john_doe",
"order_id": "12344",
"item_count": 2,
"total_amount": 49.99,
"currency": "USD"
}
{
"timestamp": "2024-01-15T10:31:12Z",
"level": "ERROR",
"message": "Payment processing failed",
"user_id": "jane_smith",
"order_id": "12345",
"amount": 129.50,
"error_code": "INVALID_CARD",
"payment_method": "visa_1234"
}
{
"timestamp": "2024-01-15T10:32:08Z",
"level": "INFO",
"message": "Order shipped",
"user_id": "bob_wilson",
"order_id": "12346",
"tracking_number": "ABC123",
"shipping_method": "express"
}
{
"timestamp": "2024-01-15T10:33:15Z",
"level": "WARN",
"message": "Low inventory alert",
"product_sku": "SKU-789",
"current_stock": 3,
"threshold": 5,
"category": "electronics"
}
BENEFITS:
✅ Easy to search: user_id="jane_smith"
✅ Easy to filter: amount>100 AND level="ERROR"
✅ Easy to aggregate: AVG(total_amount)
✅ Standard JSON parsing
✅ Consistent structure
✅ Perfect for analysis tools