What it is
A log is a stream of timestamped records, each describing one event in a running program: a request served, a retry, a failed query. Unlike metrics, which aggregate, a log line keeps the detail of the individual event, so it can answer "what happened to this request?".
Why it exists
When something goes wrong you need evidence: which requests failed, where, with what error, since when. Metrics tell you the error rate went up; logs tell you what the failures said. They are also the evidence trail for the review afterwards, and often an audit requirement.
How it works
Producing. Applications write lines to standard output (containers) or files. A collector agent on each host (Fluent Bit, Vector, the OpenTelemetry Collector) tails them, adds metadata (host, pod, service) and ships batches to a log store (Elasticsearch/OpenSearch, Loki, a cloud logging service), which indexes and retains them.
Structured lines. Instead of Order 42 failed after 812ms, write a constant message plus fields: {"level":"error","msg":"order failed","order_id":"42","duration_ms":812,"reason":"db_timeout","request_id":"7f3a9c"}. The store can then filter and group by field without regular expressions. Use consistent field names across services (OpenTelemetry semantic conventions are a good source).
Correlation. The edge (load balancer, gateway) assigns a request id, or accepts one from the caller, and every service passes it on in a header and writes it on every line. With distributed tracing in place, the trace id and span id play this role and also link logs to traces.
Levels. DEBUG for internals, INFO for normal events, WARN for handled anomalies, ERROR for failed operations. Production usually runs at INFO; many runtimes let you raise one logger to DEBUG at runtime for a short investigation.
Sampling. Keep every WARN/ERROR, sample successful INFO lines (1 in 10 or 1 in 100), and record the rate. Some pipelines sample by trace id so that a request's lines are all kept or all dropped together.
When to use it
Always, for events you might need to investigate individually: failures with their reasons, security-relevant actions, state changes, slow operations. Logs are the right tool when the question is about specific requests, users or entities.
When not to
- For counting and alerting: a metric counter is exact and fixed in cost; sampled logs are not.
- For timing across services: traces show the call tree and durations directly.
- For secrets and personal data: passwords, tokens, full card numbers and, in many jurisdictions, emails or IPs must not end up in logs, or need redaction in the pipeline.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Structured (JSON) lines | Queryable fields, consistent parsing | Less readable raw; field discipline across teams |
| Logging every success | Complete history | Volume and cost grow with traffic |
| Sampling successes | Most of the cost saved | Individual successful requests may be missing |
| Long retention | Investigate slow-burning problems | Storage cost, privacy exposure |
| Rich context fields | Faster investigations | Risk of leaking personal data; higher cardinality in the index |
Alternatives
- Metrics for counts, rates and distributions.
- Traces for request paths and latency breakdowns; span events can replace some log lines.
- Audit logs / event stores for records that must be complete and tamper-evident; do not rely on sampled application logs for those.
Failure modes
- Unsearchable logs: free-text formats that differ per team, so every query is a regex.
- Missing correlation: no request id, so a request's lines on different hosts cannot be joined.
- Log storms: an error inside a hot loop writes millions of identical lines, saturating disks, the pipeline or the budget. Rate-limit repeated messages.
- Blocking on logging: synchronous writes to a slow sink stall request threads. Log asynchronously with bounded buffers.
- Sensitive data: credentials or personal data in fields, copied into every backup of the log store.
- Lost lines: the pipeline drops data under load and nobody monitors the collector's own drop counters.
In production
Standardise a logging library and field names, include service, version, instance and request/trace id on every line, log at INFO by default, redact in the collector as a second line of defence, sample successes, and set retention by what you actually search (often days for debug detail, longer for errors and audit trails). Monitor the pipeline itself.
Common misconceptions
- "More logs means better observability." Volume without structure or correlation slows investigations and raises cost.
- "Errors on one dashboard mean one broken host." Group by instance first: an even spread usually points at a shared dependency.
- "Sampled logs are fine for error rates." Use metrics for rates; sampled logs give estimates at best.
- "DEBUG in production is harmless." It can multiply volume and expose internals or personal data.
Interview angle
Expect questions like "how would you debug a failed request across five services?" Answer with a correlation (trace) id propagated in headers and written on every structured log line, a central store, and the pattern: metrics show that errors rose, logs grouped by field show where, a single id shows the path. Mention sampling and cost, PII redaction, and that logging must never block requests.
Model assumptions
The requests, failures and their ids are simulated; log lines, the collector, the log store and sampling are scripted illustrations (the purple packets and annotations), not part of the engine. Three app servers have 8 workers each and 15 ms of own work; every request reads the orders database (6 connections, 20 ms per query, 300 ms maximum wait for a connection). The degraded database is 6× slower, so it finishes about 50 queries per second while 60 arrive. Arrivals are Poisson, service times lognormal, network hops a fixed 2–20 ms. Numbers show behaviour and orders of magnitude, not benchmarks.