Logging

Advanced PythonDebuggingFree Lesson

Advertisement

Introduction

Logging provides a way to track events during program execution, better than print statements.

Basic Logging

import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")

Configuration

logging.basicConfig(
    level=logging.DEBUG,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
    handlers=[
        logging.FileHandler("app.log"),
        logging.StreamHandler()
    ]
)

Log Levels

LevelNumericUse Case
DEBUG10Detailed debugging info
INFO20General events
WARNING30Potential issues
ERROR40Errors occurred
CRITICAL50Serious errors

Practice Problems

  1. Set up logging with file output
  2. Create logger per module
  3. Use different log levels
  4. Configure log formatting
  5. Rotate log files

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement