Structured Logging
Overview
Structured logging captures logs as machine-readable fields instead of free-form strings.
Why It Matters
- Better filtering and aggregation
- Easier correlation across services
- Cleaner alert rule definitions
flowchart TD
A[Service Log Event] --> B[JSON Formatter]
B --> C[Log Collector]
C --> D[Search Platform]
Practical Example
logger.LogInformation(
"Order created {@OrderEvent}",
new
{
OrderId = "ord-4021",
CustomerId = "cust-77",
Amount = 149.90m,
Currency = "USD"
});
Production Example
Multi-service systems commonly emit JSON logs to centralized pipelines, enabling dashboards by endpoint, tenant, and error class.
Common Pitfalls
- Mixing text and JSON formats in the same service
- Inconsistent field naming across teams
- Nested payloads that are difficult to query
Key Takeaways
- Structured logs are easier for tools and humans to analyze.
- Shared field conventions improve cross-team observability.
- Flat, stable schemas simplify long-term operations.