Logging in Python: A Comprehensive Guide

I am a Tech Enthusiast having 13+ years of experience in ๐๐ as a ๐๐จ๐ง๐ฌ๐ฎ๐ฅ๐ญ๐๐ง๐ญ, ๐๐จ๐ซ๐ฉ๐จ๐ซ๐๐ญ๐ ๐๐ซ๐๐ข๐ง๐๐ซ, ๐๐๐ง๐ญ๐จ๐ซ, with 12+ years in training and mentoring in ๐๐จ๐๐ญ๐ฐ๐๐ซ๐ ๐๐ง๐ ๐ข๐ง๐๐๐ซ๐ข๐ง๐ , ๐๐๐ญ๐ ๐๐ง๐ ๐ข๐ง๐๐๐ซ๐ข๐ง๐ , ๐๐๐ฌ๐ญ ๐๐ฎ๐ญ๐จ๐ฆ๐๐ญ๐ข๐จ๐ง ๐๐ง๐ ๐๐๐ญ๐ ๐๐๐ข๐๐ง๐๐. I have ๐๐๐๐๐๐๐ ๐๐๐๐ ๐๐๐๐ 10,000+ ๐ฐ๐ป ๐ท๐๐๐๐๐๐๐๐๐๐๐๐ and ๐๐๐๐ ๐๐๐๐๐ ๐๐๐๐ ๐๐๐๐ 500+ ๐๐๐๐๐๐๐๐ ๐๐๐๐๐๐๐๐ in the areas of ๐๐จ๐๐ญ๐ฐ๐๐ซ๐ ๐๐๐ฏ๐๐ฅ๐จ๐ฉ๐ฆ๐๐ง๐ญ, ๐๐๐ญ๐ ๐๐ง๐ ๐ข๐ง๐๐๐ซ๐ข๐ง๐ , ๐๐ฅ๐จ๐ฎ๐, ๐๐๐ญ๐ ๐๐ง๐๐ฅ๐ฒ๐ฌ๐ข๐ฌ, ๐๐๐ญ๐ ๐๐ข๐ฌ๐ฎ๐๐ฅ๐ข๐ณ๐๐ญ๐ข๐จ๐ง๐ฌ, ๐๐ซ๐ญ๐ข๐๐ข๐๐ข๐๐ฅ ๐๐ง๐ญ๐๐ฅ๐ฅ๐ข๐ ๐๐ง๐๐ ๐๐ง๐ ๐๐๐๐ก๐ข๐ง๐ ๐๐๐๐ซ๐ง๐ข๐ง๐ . I am interested in ๐ฐ๐ซ๐ข๐ญ๐ข๐ง๐ ๐๐ฅ๐จ๐ ๐ฌ, ๐ฌ๐ก๐๐ซ๐ข๐ง๐ ๐ญ๐๐๐ก๐ง๐ข๐๐๐ฅ ๐ค๐ง๐จ๐ฐ๐ฅ๐๐๐ ๐, ๐ฌ๐จ๐ฅ๐ฏ๐ข๐ง๐ ๐ญ๐๐๐ก๐ง๐ข๐๐๐ฅ ๐ข๐ฌ๐ฌ๐ฎ๐๐ฌ, ๐ซ๐๐๐๐ข๐ง๐ ๐๐ง๐ ๐ฅ๐๐๐ซ๐ง๐ข๐ง๐ new subjects.
Introduction to Logging
Logging is a crucial aspect of software development that helps developers track events, debug issues, and monitor application behavior. Instead of using
print()statements for debugging, Python's logging module provides a structured, efficient, and flexible way to record messages.
Why Use Logging Instead of print()?
Severity Levels: Logs allow categorizing messages into different levels (INFO, WARNING, ERROR, etc.).
Better Debugging: Logs persist even after execution, unlike
print(), which disappears.Custom Formatting: Logging provides timestamps, log levels, and message customization.
Multiple Output Streams: Logs can be stored in files, databases, or external monitoring services.
Basic Logging in Python
Python provides a built-in logging module that allows different log levels:
1. Importing the logging Module
import logging
2. Setting Up Basic Logging Configuration
logging.basicConfig(level=logging.INFO) # Set log level to INFO
logging.info("This is an INFO message") # Output: INFO:root:This is an INFO message
3. Logging Different Levels
Python defines multiple logging levels:
logging.debug("Debugging details") # Used for debugging information
logging.info("General information") # Used for informational messages
logging.warning("Something looks off") # Used for warnings
logging.error("An error occurred") # Used for error reporting
logging.critical("System failure!") # Used for serious failures
4. Custom Log Formatting
Customizing logs with timestamp, level, and message format:
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(message)s",
level=logging.INFO
)
logging.info("Custom formatted log message")
Example Output:
2025-05-05 12:45:30 - INFO - Custom formatted log message
Writing Logs to a File
Instead of displaying logs in the console, logs can be stored in a file:
logging.basicConfig(
filename="app.log",
format="%(asctime)s - %(levelname)s - %(message)s",
level=logging.INFO
)
logging.info("This log is saved in a file!")
The log will be written to app.log.
Using Loggers for Modular Logging
Creating a Custom Logger
logger = logging.getLogger("custom_logger") # Create a named logger
logger.setLevel(logging.DEBUG)
# Create a handler for logging to a file
file_handler = logging.FileHandler("custom.log")
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.debug("This debug log is recorded in custom.log")
This method ensures modular logging across different parts of a program.
Using Logging in a Real-World Application
Example: API Request Logging
import logging
import requests
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO)
def fetch_data(url):
logging.info(f"Fetching data from {url}")
try:
response = requests.get(url)
response.raise_for_status() # Raise error for failed requests
logging.info(f"Received response: {response.status_code}")
return response.json()
except requests.exceptions.RequestException as e:
logging.error(f"Request failed: {e}")
data = fetch_data("https://api.github.com")
Example Output
2025-05-05 12:50:15 - INFO - Fetching data from https://api.github.com
2025-05-05 12:50:15 - INFO - Received response: 200



