Learn with diagrams, code, systems and practical examples.
Debugging, Logging, and Profiling
Find why code is wrong before trying to make it faster.
Good engineering means understanding failures and performance with evidence rather than guessing.
Start with the idea
Debugging traces incorrect behavior, logging records useful runtime context, and profiling measures where time or resources are actually spent.
Small working example
Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.
import logging
logging.basicConfig(level=logging.INFO)
user_id = 42
logging.info("processing user %s", user_id) Debugging traces incorrect behavior, logging records useful runtime context, and profiling measures where time or resources are actually spent.
- Debug with evidence.
- Logs should be useful and safe.
- Profile before optimizing.
The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.
Common beginner mistake
Adding many print statements that expose sensitive values or remain in production code forever.
Use structured, intentional logging and avoid secrets or personal data that are not required for diagnosis.
Quick recap
- Debug with evidence.
- Logs should be useful and safe.
- Profile before optimizing.
Try it yourself
These are deliberately small. If you can complete them without copying the example, you are ready to continue.
- Add one useful log around a function call.
- Use a debugger or traceback to inspect an intentional error.