Topic 3 of 5 · 5 min read
Reading stack traces and log timelines
Goal: Locate the originating error in a stack trace and a log excerpt and explain the chain of events that led to the visible failure.
A stack trace shows where the program was when it failed; a log timeline shows what happened before. Read both for the first thing that went wrong, not the last thing that was reported. The loudest error is often a side effect.
Check yourself · 1 of 3
In a long stack trace, where do you usually start reading the frames?
Why it matters
Pasting a 200-line trace into a chat and asking "why?" skips the reading that finds the answer. The trace usually holds the answer in two or three lines, and knowing which ones lets you judge any explanation, human or AI.
Reading a stack trace
- Read the message and type first.
TypeError: Cannot read properties of undefined (reading 'id')already says what failed: something was undefined where an object was expected. - Find the first frame in your code. Frames from libraries and the runtime are usually noise. The topmost frame in your own files is the best place to start. In Python the most recent call is last; in JavaScript and Java it is first. Check which.
- Look for the cause chain. Wrapped errors carry the original under
Caused by:(Java),Error.cause(JavaScript) orThe above exception was the direct cause(Python). The deepest cause is usually the real fault. - Allow for async. After an
awaitor a callback, the trace may not show who called you. Enable async stack traces, or log the request id.
Reading a log timeline
Filter all lines for one request id or trace id, sort by timestamp, and read from the top. Ask: what is the first line that is not normal? Errors later in the timeline are often consequences. Check clocks too: lines from two servers can be a few hundred milliseconds out of order.
12:03:01.112 req=a91 INFO POST /orders user=42
12:03:01.130 req=a91 WARN inventory timeout after 2000ms, retry 1
12:03:03.141 req=a91 ERROR payment: card token undefined
12:03:03.142 req=a91 ERROR POST /orders 500
The 500 and the payment error are visible. The first abnormal line is the inventory timeout; the retry delay probably let the checkout session expire, and the payment code then read an empty token.
Before and after
Weak: "It's a payment bug", because the payment line sits next to the 500.
Better: "The first fault is the inventory timeout at 12:03:01.130. The payment error comes 2 seconds later. Check what the retry does to the session."
What goes wrong
- Reading from the bottom only. The last line is the symptom.
- Stopping at the wrapper. A generic
RuntimeErrorwrapping a database error hides the real one unless you read the chain. - Mixing requests. Without an id filter, lines from other users look like part of the story.
- Trusting log levels. A harmless-looking INFO line can be the first abnormal event; a noisy WARN may be normal.
Examples
The wrapped database error
Error: could not save order
at saveOrder (orders.ts:48:11)
[cause]: error: duplicate key value violates unique constraint "orders_ref_key"
at pg (connection.js:291:20)
The outer message is generic. The cause names the constraint: the order reference was generated twice. The question is how, not how to save an order.
Same request, two servers
A line from server B at 12:03:01.090 appears before the request start from server A at 12:03:01.112. The clocks differ by about 30 ms. Order by the request id and sequence numbers, not by timestamp alone, when the gaps are this small.
Key points
- Read the error type and message first, then find the first frame in your own code.
- Follow the cause chain to the deepest wrapped error before deciding what failed.
- Filter logs by request or trace id and read in time order from the first abnormal line.
- Later errors in a timeline are often consequences of an earlier one.
- Async calls and multiple servers can hide the caller or reorder lines; use ids and check clocks.
Common mistakes
- Starting at the last error line instead of the first abnormal one.
- Ignoring the cause chain and treating the wrapper message as the fault.
- Reading unfiltered logs where several requests are interleaved.
- Assuming the top frame is always the cause, when it may only be where a bad value was finally used.
Check yourself · 2 of 3
A trace shows a generic error wrapping another error under 'Caused by'. Which do you investigate first?
Check yourself · 3 of 3
Logs for one request show a timeout at 12:03:01, then a null error at 12:03:03, then a 500. What is the most useful reading?
Practice items, not counted towards your mastery.
Start here
- Debugging and Profiling (MIT Missing Semester) (opens in a new tab)CourseFree
Print debugging, logging, debuggers and reading traces, taught with short exercises.
- A debugging manifesto (Julia Evans) (opens in a new tab)ArticleFree
A short list of habits: reproduce first, simplify, and test your assumptions.
- The Debugging Book (opens in a new tab)BookFree
Free book on tracing, reducing failing inputs and finding root causes automatically.
More for this topic
- Error: cause (MDN) (opens in a new tab)DocsFree
How wrapped errors keep the original failure, so you can read the chain.
- traceback module (Python docs) (opens in a new tab)DocsFree
How Python formats stack traces and chained exceptions.