
Bug hunt
The Quiet Logbook
print is fine for quick checks, but real programs use logging.
Every message has a level, and you choose which levels to show:
DEBUG < INFO < WARNING < ERROR < CRITICAL
import logging, sys
logging.basicConfig(level=logging.INFO, stream=sys.stdout,
format="%(levelname)s:%(name)s:%(message)s",
force=True)
log = logging.getLogger("depot")
log.debug("hidden: below INFO")
log.info("Loaded %d crates", 3) # INFO:depot:Loaded 3 crates
level=hides every message below that level.- Pass values as extra arguments (
"%d crates", 3). Every%placeholder needs a matching value. stream=sys.stdoutsends logs to PyQuest's output panel, andforce=Truereplaces any setup from an earlier run. In a real app you callbasicConfigonce, at startup.
The bug report
The depot's log should show:
INFO:depot:Loading 3 crates
ERROR:depot:Crate wick has negative qty -2
Total: 9
Instead, the INFO line never appears. And the error turns into a
scary --- Logging error --- message.
Assignment
Fix the two bugs:
- Change the
levelinbasicConfigso INFO messages are shown. - Fix the
log.error(...)call so every%placeholder gets a value.
The program should then print exactly the three lines above.
Fix the two bugs:
- Change the
levelinbasicConfigso INFO messages are shown. - Fix the
log.error(...)call so every%placeholder gets a value.
The program should then print exactly the three lines above.
main.py Python sleeping
Press Run to try your code, then Check to test it.