Module 20 · Building Real Python Applications — topic 11 of 13
Debugging applications
Debugging beyond single functions: configuring logging per component, reproducing problems from recorded input, isolating the failing step, and bisecting data to find the record that breaks a report.
Learning objectives
- Configure logging for different components
- Reproduce bugs by replaying recorded input
- Narrow down failing data by bisection
Definition
Debugging an application means locating faults in a system of interacting components. It relies on logs that show what each component did, reproducible inputs, and techniques for narrowing a problem down to one component, step or record.
Introduction
'The monthly report crashes for one user' is a typical application bug: the code worked in tests, but some combination of data and steps breaks it.
Why it matters
Application bugs are often intermittent or data-dependent. Logging, replay and bisection turn them into ordinary, reproducible bugs.
Detailed explanation
Give each component its own logger (logging.getLogger("tracker.storage")) so you can turn on detailed logs for one part only.
Log important events with context: which command, which file, how many records — but never passwords or personal data.
Record the inputs that lead to a bug (commands, data files) so the problem can be replayed exactly, again and again.
Replay the steps one by one and report the first step that fails, with its input and exception.
Isolate the component: if storage loads correct data, the bug is in the logic or output; test each layer separately.
When a large data set breaks processing, bisect it: run on half the data, keep the failing half, and repeat until one record remains.
Once found, write a failing test with that minimal input before fixing.
Keep debugging aids (log statements, replay scripts) — they will help with the next bug.
Syntax
- One logger per component.
- Replay recorded input.
- Bisect failing data.
Code examples
Logging per component
tracker.storage DEBUG loading expenses.json tracker.ui INFO showing 3 expenses
Replaying recorded input
step 2 'add x travel' failed: ValueError: could not convert string to float: 'x'
More examples
Bisecting data to find a bad record
bad record at index 63 {'amount': 'N/A'} found in 7 checksStep-by-step explanation
- The report crashes somewhere among 100 records.
- Each check runs the report on half of the remaining range and keeps the half that fails.
- Seven checks pinpoint the one bad record — the same halving idea as binary search.
Expected output
Running “Bisecting data to find a bad record” prints exactly:
Practical applications
- Finding the one malformed row in a large import.
- git bisect finding the commit that introduced a bug.
Best practices
- Use named loggers per component.
- Record and replay failing inputs.
- Bisect large data sets.
Common mistakes
Logging personal data
Avoid
Prefer
Logs are often shared and stored; keep secrets and personal data out.
Important notes
- Error-tracking services collect exceptions with context from deployed applications automatically.
Key takeaways
- Per-component logging shows what each part did.
- Replay recorded input to reproduce bugs.
- Bisection finds failing records quickly.
Try it yourself
- 1Add DEBUG logging to the storage layer of your tracker and switch it on with an environment variable.
- 2Write a replay script that reads commands from a file and reports the first failure.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
8 questions cover the “Building Real Python Applications” module. Answers and explanations are revealed after you submit.
- AdvancedWhat does MVP stand for when planning an application?
- AdvancedWhere should input validation ideally happen?
- AdvancedWhat is printed?