Lesson 32 / 49
Logging & Debugging
Replace print debugging with the logging module and step through code with pdb and breakpoint().
By Rahul Agarwal · 15 years in web development · Updated 1 Oct 2026
The logging module
logging adds levels, timestamps and destinations, and can be turned up or down without editing code.
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger(__name__)
log.info("Starting job")
log.warning("Disk at %d%%", 91)
try:
1 / 0
except ZeroDivisionError:
log.exception("Calculation failed")breakpoint() and pdb
breakpoint() drops you into the debugger at that line. Use n (next), s (step), c (continue) and p expr (print).
def total(prices):
result = 0
for p in prices:
breakpoint() # inspect p and result here
result += p
return resultUse levels deliberately: DEBUG for detail, INFO for milestones, WARNING for odd events, ERROR for failures. Never log passwords or tokens.