What to log, how to structure it, and what to never write down.
A log is a line your program writes to say "this just happened". Logging best practices are the habits that make those lines useful when something breaks, and safe to keep.
Logs are the diary of your program. When a customer says "my order failed at 10:05", the log is often the only record of what really happened. But a diary full of noise, or one that contains passwords, is worse than no diary. Good logging is a small set of habits.
Every log line should answer: when (timestamp, ideally in UTC), how serious (level), who wrote it (service, version, host), what happened (a clear message) and in what context (request ID, user ID, order ID and other fields).
| Level | Use it for | Example |
|---|---|---|
| DEBUG | Detail for developers. Usually off in production. | Loaded cart with 3 items |
| INFO | Normal, meaningful events | Order placed, total 42.50 |
| WARN | Something odd that the system handled, but should be looked at | Slow gateway reply (2300 ms), retrying |
| ERROR | An operation failed and needs attention | Card declined, order failed |
| FATAL / CRITICAL | The process cannot continue | Cannot connect to the database, shutting down |
{"ts":"2026-12-03T10:00:08Z","level":"ERROR","service":"payment","version":"1.4.2",
"req":"r-102","user":"u-52","msg":"Card declined","code":"insufficient_funds"}Twelve made-up events from two checkout requests that overlap in time. Sizes are for this small sample only and only show the direction: more levels and more fields mean more bytes.
| Term | Meaning in easy words |
|---|---|
| Log level | A label for how serious a line is: DEBUG, INFO, WARN, ERROR |
| Structured logging | Writing logs as named fields (usually JSON) instead of only free text |
| Request / correlation ID | A unique ID carried through a request, so its lines can be found together |
| Context | Extra fields such as user ID or order ID that explain a line |
| Audit log | A protected record of who did what and when, kept for security and rules |
| PII | Personally identifiable information, such as an email or a phone number |
| Masking / redaction | Hiding part or all of a sensitive value before it is logged |
| Sampling | Writing only a share of very frequent lines |
| Log rotation | Starting a new file when one is full or old, and removing the oldest |
| Retention | How long logs are kept before they are deleted |
| Stack trace | The list of calls that led to an error, often printed with an exception |
| Choice | We gain | We lose |
|---|---|---|
| DEBUG in production | Maximum detail for an investigation | A very large volume, higher cost and more risk of leaking data |
| INFO and above | A calm, affordable log | Less detail unless you raise the level |
| JSON logs | Searchable by field, easy for tools | Harder for a person to read in a terminal |
| Plain text logs | Easy to read by eye | Fragile pattern matching, and fields are hard to filter |
| Log at every layer | Never miss where it failed | Duplicate lines and noise |
| Sample frequent lines | Much lower volume | You may miss a rare event in the dropped part |