Logging in Python

The logging module in Python allows developers to track events that happen during the execution of a program. It’s an essential tool for debugging, monitoring, and understanding how the code behaves at runtime. Unlike using print statements, logging provides a flexible framework for categorizing, formatting, and routing log messages.

Why Use Logging?

Using the logging module has several advantages over simple print statements:

Setting Up a Basic Logger

To set up basic logging, use the basicConfig() method:

Python
import logging

# Create a basic logger that outputs to the console
logging.basicConfig(level=logging.INFO)
logging.info("This is an info message")

Explanation:

The basicConfig() method configures the default logging behavior. In this example, we set the logging level to INFO, meaning all log messages of level INFO and above will be displayed.

Logging Levels

There are five standard logging levels in Python:

Customizing Log Format

You can customize how the log messages appear using the format parameter in basicConfig():

Python
import logging

# Customize the log format
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
logging.info("This message has a custom format")

Explanation:

In this example, we’ve customized the log output format to include the timestamp (asctime), log level (levelname), and the log message (message).

Logging to a File

Instead of printing logs to the console, you can write them to a file:

Python
import logging

# Log messages to a file
logging.basicConfig(filename='app.log', filemode='w', level=logging.INFO)
logging.info("This message will be written to the log file")

Explanation:

The filename parameter specifies the log file’s name, and filemode='w' ensures that the log file is overwritten each time the program runs. Use filemode='a' to append to the log file instead.

Using Handlers

Handlers allow you to send logs to different destinations, like files or external services:

Python
import logging

# Create a custom logger
logger = logging.getLogger('my_logger')

# Create a file handler
file_handler = logging.FileHandler('my_log.log')

# Set formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)

# Add handler to logger
logger.addHandler(file_handler)
logger.setLevel(logging.INFO)

logger.info("Logging to the file using handlers")

Explanation:

In this example, we use a FileHandler to log messages to a file with a custom format. The logger is configured with a handler that formats and routes log messages.

Key Points to Remember