← Back to Python

All Topics

Advertisement

Learn/Python/Advanced Python

Logging

Topic: Debugging

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

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →