Skip to main content

Logging in Python: A Comprehensive Guide

Updated 2 min readMarkdown
Logging in Python: A Comprehensive Guide

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()?

  1. Severity Levels: Logs allow categorizing messages into different levels (INFO, WARNING, ERROR, etc.).

  2. Better Debugging: Logs persist even after execution, unlike print(), which disappears.

  3. Custom Formatting: Logging provides timestamps, log levels, and message customization.

  4. 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

About the author

  • 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.

No comments yet. Be the first to comment.

More from this blog