Skip to content

Logging for Automation Scripts (logging module)

Logs help you answer:

  • what happened?
  • when did it happen?
  • why did it fail?
logging_to_file.py
import logging
 
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s %(message)s",
    handlers=[
        logging.FileHandler("job.log", encoding="utf-8"),
        logging.StreamHandler(),
    ],
)
 
log = logging.getLogger("job")
log.info("started")

For large logs, use RotatingFileHandler.

Exercise 2 – Join Paths with os.path.join

Section titled “Exercise 2 – Join Paths with os.path.join”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading