Python22 min read

Python Logging

Add professional logging to your apps: track errors, understand behavior in production, and replace print() with structured logs and log files.

David Miller
August 6, 2025
3.9k121

Logging is how real applications “talk” about what they are doing.

        Why logging is important:
        - in production you cannot rely on print()
        - logs help you debug issues after they happen
        - logs help you see user behavior and system failures
        
        ## Logging levels (simple meaning)
        
        - DEBUG: detailed developer info
        - INFO: normal operations
        - WARNING: something unexpected but not fatal
        - ERROR: operation failed
        - CRITICAL: app is in danger
        
        ## Basic logging
        
        ```python
        import logging
        
        logging.basicConfig(level=logging.INFO)
        
        logging.debug("Debug message")
        logging.info("Info message")
        logging.warning("Warning message")
        logging.error("Error message")
        logging.critical("Critical message")
        ```
        
        ## Log to a file (production friendly)
        
        ```python
        import logging
        
        logging.basicConfig(
            filename="app.log",
            level=logging.INFO,
            format="%(asctime)s - %(levelname)s - %(message)s"
        )
        
        logging.info("Application started")
        logging.error("An error occurred")
        ```
        
        ## Multiple handlers (console + file)
        
        ```python
        import logging
        
        logger = logging.getLogger(__name__)
        logger.setLevel(logging.DEBUG)
        
        console_handler = logging.StreamHandler()
        console_handler.setLevel(logging.INFO)
        
        file_handler = logging.FileHandler("errors.log")
        file_handler.setLevel(logging.ERROR)
        
        formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        )
        console_handler.setFormatter(formatter)
        file_handler.setFormatter(formatter)
        
        logger.addHandler(console_handler)
        logger.addHandler(file_handler)
        
        logger.info("Visible in console")
        logger.error("Saved in errors.log and also shown if console level allows")
        ```
        
        ## Log full exception details
        
        ```python
        import logging
        
        logging.basicConfig(level=logging.ERROR)
        
        try:
            10 / 0
        except Exception:
            logging.error("Crash happened", exc_info=True)
        ```
        
        ## Graph: where logs go
        
        ```mermaid
        flowchart LR
          A[Your code] --> B[logger.info/error]
          B --> C[Console]
          B --> D[Log file]
          D --> E[Monitoring / Debugging]
        ```
        
        ## Remember
        
        - Use logging instead of print() in real apps
        - Log files are critical for production debugging
        - Never log secrets (passwords, tokens, API keys)
        
#Python#Advanced#Logging