Structured Logging

Stop writing console.log("User created: " + userId). Use structured logging: {"event": "user_created", "user_id": "abc123", "timestamp": "..."}. Structured logs are queryable, filterable, and parseable by log aggregation tools. Every major language has a structured logging library — pino (Node.js), slog (Go), structlog (Python), serilog (C#).

Log Levels Done Right

DEBUG: Detailed diagnostic info (disabled in production). INFO: Normal operations — user actions, service starts, config loaded. WARN: Unexpected but recoverable — degraded service, retry attempts. ERROR: Failures that need attention — failed API calls, database errors. FATAL: System cannot continue — unrecoverable errors, crashes.

Correlation IDs

In microservice architectures, a single user request may touch 5+ services. A correlation ID (usually a UUID) propagated through headers lets you trace the entire request chain. Generate it at the API gateway, pass it through every service, include it in every log entry.

What to Log (and What Not To)

Log: request/response metadata, error details with stack traces, business events, performance metrics. Don't log: passwords, API keys, tokens, session IDs, PII (emails, addresses), or full request bodies containing sensitive data. Use redaction libraries to automatically mask sensitive fields.

Log Aggregation Tools

Grafana Loki: Log aggregation designed for Grafana. Cost-effective, labels-based indexing. ELK Stack (Elasticsearch, Logstash, Kibana): Powerful full-text search, resource-heavy. Datadog: Managed solution with excellent UX but expensive at scale. For small teams, Loki + Grafana provides the best cost-to-value ratio in 2026.

Performance Considerations

Async logging prevents I/O blocking your application. Buffer logs and flush in batches. Set appropriate log levels per environment — DEBUG in development, INFO/WARN in production. Implement log rotation to prevent disk exhaustion. Consider sampling high-volume logs (e.g., log 10% of successful requests, 100% of errors).

Conclusion

Good logging is invisible until something breaks — then it's the difference between a 5-minute fix and a 5-hour investigation. Use structured logs, appropriate levels, correlation IDs, and centralized aggregation. Your future self debugging a production incident will thank you.