Skip to main content

24. Logging Best Practices

Logging provides visibility into what your code is doing, especially in production.


Basic Logging

import logging

logging.basicConfig(level=logging.INFO)
logging.info("This is an info message")
logging.error("This is an error message")

Logging Levels

  • DEBUG: detailed information for diagnosing issues
  • INFO: confirmation that things are working
  • WARNING: something unexpected happened, still running
  • ERROR: a serious issue occurred
  • CRITICAL: severe error, program may not continue

Practice Challenge

Configure logging to write to a file app.log and test it with different levels of messages.


Wrap-Up

Logging is more flexible and powerful than using print statements.