How to Use Logging in Python

09/08/2021

Contents

In this article, you will learn how to use logging in Python.

Logging in Python

Python provides the built-in logging module for logging messages, with different levels of severity (e.g. DEBUG, INFO, WARNING, ERROR, CRITICAL).

Here’s an example of how to use logging in Python:

import logging

# Basic configuration of logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s',
                    datefmt='%m/%d/%Y %I:%M:%S %p',
                    filename='example.log',
                    filemode='w')

# Example of logging messages
logging.debug("Debug message")
logging.info("Informational message")
logging.warning("Warning message")
logging.error("Error message")
logging.critical("Critical message")

You can change the level of severity to log only certain levels of messages, and you can also specify different handlers to send log messages to different places, such as a file, the console, or a remote log server.

 

Here are some more details about the logging module:

Logging Levels

The logging module defines five levels of severity for messages, from most to least severe: DEBUG, INFO, WARNING, ERROR, and CRITICAL. You can set the level of severity that you want to log when you configure the logging module, and messages with a severity level lower than the set level will be ignored.

Handlers

By default, log messages are sent to the console, but you can also send them to other destinations, such as a file or a remote log server, by using handlers. For example, you can use the FileHandler class to send messages to a file, or the SMTPHandler class to send messages via email.

Formatters

The logging module provides a way to specify the format of log messages through formatters. A formatter specifies the structure of a log message, including its date and time, the severity level, the name of the logger that generated the message, and the actual message itself. You can create your own formatters or use the built-in formatters, such as BasicFormatter.

Loggers

Loggers are the objects that emit log messages. A logger has a name, and you can use this name to configure its behavior and to access it in your code. The logger provides methods for emitting log messages, such as debug, info, warning, error, and critical.

Propagation

When a logger emits a log message, it first checks if there is a handler attached to it. If there is no handler, the logger will check if its parent logger has a handler. This process continues until a handler is found or the root logger is reached. This allows you to configure loggers in a hierarchical manner, with parent loggers passing on log messages to child loggers if they don’t have a handler themselves.

Filter

Filters allow you to further customize the handling of log messages by adding conditions to determine if a message should be emitted or not. Filters can be added to handlers to affect all log messages sent to that handler, or to loggers to affect only messages emitted by that logger.