Logs are the only evidence of what actually happened rather than what the code says should happen. That makes them the strongest input available for debugging, and they are usually either absent, unreadable or pasted in unhelpful quantities.
What should you paste?
The window around the failure: a short period before, the failure itself, and what came after. Not the whole file, and not only the error line, since the lines before it are where the cause usually is.
Here is the log from thirty seconds before the failure to ten seconds after, and here is the code that produces these messages. What sequence of events does this show, and where does it diverge from what should have happened?
Why ask for the sequence?
Because reconstructing what happened is a comprehension task, which is what these tools do well. A log showing that something ran twice, or ran in an unexpected order, is a diagnosis rather than a clue.
What makes logs usable?
- A request identifier. So one journey can be followed.
- Timestamps with enough precision. To establish order.
- The values involved. Not just that something happened.
- Consistent structure. So they can be filtered and searched.
- A level. So the important ones can be separated.
Why is the request identifier so important?
Because logs from a live application interleave requests from different people. Without something tying lines together, you are reading a conversation with several people talking at once, and no amount of analysis untangles it.
This is the single highest-value addition to logging in any application.
What should you log?
Enough to answer what happened without reproducing it. Entry and exit of significant operations, the values that decisions were based on, and anything that failed including what it was attempting.
Logging every line produces noise; logging only errors leaves you unable to see what preceded them.
What should never be logged?
Credentials, tokens, full personal records and anything a user submitted that might be sensitive. Logs are copied, shared and pasted, and this is where personal data most often ends up somewhere nobody intended.
Checking what your logs actually contain is a ten-minute job with a frequently uncomfortable result.
How do you improve logging after an incident?
By asking what you would have needed. The most valuable moment to improve logging is immediately after an investigation that was harder than it should have been, since you know exactly what was missing.
What about structured logging?
Worth adopting once an application is running for real, since machine-readable entries can be filtered by field rather than searched as text. It also makes pasting an excerpt considerably more informative.
What about log levels?
Worth using properly rather than logging everything at one level. A system where errors, warnings and routine information are indistinguishable produces a volume nobody reads, which is the same outcome as not logging at all.
The practical arrangement is to log detail freely at a level that is filtered out normally and can be turned on during an investigation. That gives you depth when you need it without the noise the rest of the time, and it avoids the common pattern of adding logging during a problem and removing it afterwards.
What is the habit?
Read the log before asking anything. A surprising proportion of problems are visible in it directly, and the ones that are not become a much better question once you can say what the log shows.