Logging best practices: what to log, how to log it, and what never to log
Logs are often the only way to understand what happened in production. Poor logging is either too quiet to help or so noisy that the important line gets buried.
What to log:
- Important business events (order placed, payment failed, user signed up)
- Errors and exceptions with stack traces and context
- External calls that fail or are slow
- Startup configuration (non-sensitive parts) and version information
How to log well:
- Structured logging: emit JSON or key-value fields instead of free text, so logs can be filtered and queried.
- Consistent levels: DEBUG for development detail, INFO for normal events, WARN for unexpected but handled, ERROR for failures needing attention.
- Correlation IDs: attach a request or trace ID so you can follow one request across services.
- Actionable messages: include what failed, with which inputs, and what happens next.
What never to log:
- Passwords, tokens, API keys and session cookies
- Full payment card numbers or government ID numbers
- Sensitive personal data unless you truly need it and have a retention policy
- Raw request bodies by default; they often contain all of the above
Other considerations:
- Logging costs money at scale; sample high-volume debug logs
- Set retention periods appropriate to legal and privacy requirements
- Logs complement metrics and traces rather than replacing them
What's the most useful log line you've ever added? Have you found secrets in logs during an audit?
Comments · 3
What's the harm in logging everything? Beginners like me would rather have too much than too little.
Never log passwords, tokens or personal data. We had to scrub months of logs after a request body got dumped by accident.
Structured logs with consistent fields make searching tractable. It's the difference between a log you can query and a pile of prose.