Instrumentation: logs, breakpoints and assertions
Turn a vague failure report into a reproduction, isolate the cause, form and falsify hypotheses, instrument the code, fix async and state bugs, lock the fix behind a regression test and review changes for real trade-offs.
What you will be able to do
#- Choose between a breakpoint, a structured log and an assertion for a given bug.
- Add instrumentation that reveals values without flooding production.
Overview
#You cannot fix what you cannot see. Instrumentation is how you make the invisible visible: logs, breakpoints, assertions and timers.
Start with the least invasive tool. A breakpoint in a debugger pauses execution and lets you inspect every variable in scope without editing code. A console log is faster to add but litters the code and disappears from production builds if stripped.
Log values, not sentences. Printing the object is more useful than printing a word like here, because it shows the actual shape. Use console.table for arrays of objects and console.trace to capture a call path.
function checkout(cart) {
console.log('cart', cart);
const total = cart.items.reduce((sum, i) => sum + i.price, 0);
console.log('total', total);
return total;
}
For intermittent bugs, a breakpoint is useless because it stops the process. Use structured logs with a correlation id so you can reconstruct the sequence after the fact.
function checkout(cart, requestId) {
logger.info({ requestId, itemCount: cart.items.length }, 'checkout started');
const total = cart.items.reduce((sum, i) => sum + i.price, 0);
logger.info({ requestId, total }, 'checkout computed');
return total;
}
Assertions catch violated assumptions at the point they break, not three functions later. Use them for invariants you believe must always hold.
function withTax(subtotal, rate) {
if (rate < 0 || rate > 1) throw new RangeError('rate must be between 0 and 1');
return subtotal * (1 + rate);
}
Finally, time the suspicious region before you optimise it. console.time and performance.now turn a feeling into a number you can compare.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Add minimal instrumentation to checkout so a failing run reveals the input, the computed total and the final return value. Keep the logs structured so they can be removed or kept in production.
starter.javascriptfunction checkout(cart) { const total = cart.items.reduce((sum, i) => sum + i.price, 0); return total; }Reveal the reference answer
reference.javascriptfunction checkout(cart, requestId) { logger.info({ requestId, itemCount: cart.items.length }, 'checkout started'); const total = cart.items.reduce((sum, i) => sum + i.price, 0); logger.info({ requestId, total }, 'checkout computed'); return total; }Authored expected output: Two structured log lines: one at entry with the item count, one after the total is computed with the total value.
Structured fields carry the values, and the request id lets you join the two lines with the rest of the request. A plain sentence log would not be searchable.
Exercise 2Predict
An intermittent bug appears once every few hours. Which tool is most useful?
Common misconceptions
#- A breakpoint is the best tool for a bug that happens once every few hours.
- More log lines always means more information.
Follow-up practice
#- debugging-review-06-state-bugs
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.