Architecture & opsUpdated 2026-11-20

Logging

Structured logs with correlation ids, sensible levels, and the things that must never be logged.

The shop diary. A written note of everything that happened and when.

That is a log. When something goes wrong at three in the morning, the diary is how you find out what the system was doing, in order, without having to guess.

How it works

Logs answer one question: what happened?

Code
2026-11-20T09:14:02Z  error  payment.failed  user=123  request=a81f  reason=card_declined

A few practices turn a pile of text into something you can use.

  • Structured logging. Write JSON, so each line is a record with named fields. You can then query for every payment.failed event for one user instead of searching free text.
  • A request id on every line. Generate or accept an id at the edge and carry it through every log line for that request. One search then returns the whole story, even across several services.
  • The right level.
    • error for actual failures.
    • warn for recoverable problems.
    • info for key business events.
    • debug for local troubleshooting, usually off in production.
  • No secrets. Never log passwords, tokens or full payment details.
Code
Request a81f
  ├── api      info   order.created
  ├── worker   warn   email.retry attempt=2
  └── api      error  payment.failed

What to log

Log events that someone will want to read later: a sign-in, a failed payment, a job that retried, a permission denied. Include the identifiers that make the line useful, such as tenant, user, request and the object affected. A line without an identifier says something happened somewhere.

What not to log

Anything that would be harmful if the log leaked. Logs are copied to other systems, kept for a long time and read by more people than the database is. Redact personal data, and treat the log store with the same care as any other store of customer information.

Logs next to the rest

Logs say what happened. Metrics say how often, and traces say where the time went. Together they form observability. A metric usually tells you something is wrong, and the log tells you why.

Searching during an incident

An incident is the test of a logging setup. The question is usually narrow: what happened to this request, for this user, in the last ten minutes. If you can filter by request id and tenant and read the events in order, the log is working. If you have to grep free text across several machines, fix that before the next incident. Keeping the same field names across every service matters more than the choice of tool. A team that agrees on names such as request, tenant and user once can query any service the same way, and a new service inherits the habit for free.

Where I used it

I haven't shipped this in a public project yet.

None of my case studies describe a logging setup, so I will not claim one. This note is the practice I would follow, and the request id, structured fields and redaction rules are the parts I would set up first.

Trade-offs

  • Volume. Logging everything is expensive to store and slow to search. Sample the noisy debug lines and keep the important ones.
  • Cost of retention. Decide how long to keep logs. Longer helps investigations and raises both cost and privacy exposure.
  • Free text rots. An unstructured message changes with every edit and breaks every query that depended on it.
  • Logs can leak. One careless log of a request body can put tokens or personal data in a place nobody protects.
  • Logging is not alerting. A line nobody reads does nothing. Pair logs with alerts on the failures that matter.
  • Performance. Synchronous logging on a hot path adds latency. Buffer or send logs asynchronously.

A useful rule: every service logs, always. Make the lines structured, give them a request id and leave out anything you would not want read aloud.