Web Analytics

Logging

Advanced ~10 min read

Using print() statements for debugging is common, but for production applications, you should use the logging module. It provides a flexible way to track events.

Log Levels

Logging has different levels of severity:

  • DEBUG: Detailed info, for diagnosing problems.
  • INFO: Confirmation that things are working.
  • WARNING: Indication that something unexpected happened.
  • ERROR: Due to a more serious problem, the software has not been able to perform some function.
  • CRITICAL: A serious error, indicating that the program itself may be unable to continue running.
Output
Click Run to execute your code

Exercise: Add Logging

Task: Add logging to the script.

Requirements:

  • Configure logging to DEBUG level.
  • Add an INFO log when processing data.
  • Add a WARNING log if data is empty.
Output
Click Run to execute your code
Show Solution
logging.basicConfig(level=logging.DEBUG)
logging.info("Processing data")
logging.warning("Data is empty")