You wrote a script, put it on a cron job, and one night it fails. You open the output and find a wall of print() lines with no timestamps, no severity, and no idea which line came from which step.
Python's built-in logging module fixes this, and you do not need to install anything. Here is the smallest setup that is actually worth using.
Step 1: replace print with a configured logger
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
log = logging.getLogger(__name__)
log.info("starting job")
log.warning("config file missing, using defaults")
log.error("could not reach the API")
Output:
2026-10-25 16:40:02,114 INFO __main__: starting job
2026-10-25 16:40:02,114 WARNING __main__: config file missing, using defaults
2026-10-25 16:40:02,115 ERROR __main__: could not reach the API
Every line now has a time, a level, and the module that wrote it.
Step 2: log to a file that does
Discussion
Jump in and comment!
Get the ball rolling with your comment!