What are effective Centralized Logging strategies?
Topic: microservices, Observability. Level: Intermediate.
Answer
With many ephemeral instances, `ssh`-into-a-box logging is impossible. Ship all logs to a central store (ELK/EFK, Loki, Splunk). Best practices: **structured JSON logs** (queryable fields, not free text), a **correlation/trace id** on every line, consistent levels, no secrets/PII, and context fields (service, version, env). Logs, metrics, and traces are the "three pillars" — link them by trace id. Sample or rate-limit noisy logs to control cost.
```java
// Structured log with correlation id -> searchable in Kibana/Loki by traceId
log.atInfo()
.addKeyValue("event", "order.placed")
.addKeyValue("orderId", order.getId())
.addKeyValue("traceId", MDC.get("correlationId"))
.addKeyValue("amountCents", order.getTotalCents())
.log("Order placed");
// -> {"level":"INFO","event":"order.placed","orderId":"A1","traceId":"...", ...}
```