Logging records what a program is doing while it runs.
Unlike print, logs can include levels such as debug, info, warning, and error.
Use logging when a script or application needs useful runtime history without mixing debug text into normal output.
Levels describe how important a message is. Debug messages are detailed; error messages show real failures.
import logging
logging.basicConfig(level=logging.INFO)
logging.info("Starting report")
logging.warning("No category filter selected")
INFO:root:Starting report
WARNING:root:No category filter selected
The level controls which messages appear.
logging.exception records a message plus the traceback for the current exception.
import logging
value = "free"
try:
price = int(value)
except ValueError:
logging.exception("Could not convert price: %s", value)
The log includes the message and traceback, which is better than a vague print.
Try this next
0 of 3 completed
Use print for program output learners or users should see. Use logging for diagnostic information about the program itself.
INFO is a good starting level because it shows useful progress without showing every tiny detail.
No. Logging helps you see what happened. You still need validation, error handling, or code changes to fix the cause.
Explore 500+ free tutorials across 20+ languages and frameworks.