How To Configure Logging In Python?

How To Configure Logging In Python

Logging is important for recording the operational data of applications and providing transparency during runtime. This article guides you through the detailed steps of configuring logging in Python, using the built-in ‘logging’ module to ensure accurate and effective logging practices. Logging is implemented by considering various features such as logging levels, handlers, log message formats, and filters. The best way to do this is that we will learn compliance of Python with all the logging mechanisms.

Configuring Logging In Python

Configuring logging in Python is a process that involves several steps. These steps are the following:

  1. Import logging module
  2. Configure logging 
  3. Use of loggers 
  4. Configuring Handlers 
  5. Formatter/layout configuration
  6. Define logging levels

Let’s look at these steps in detail, one by one. 

1. Import Logging Module

The Python logging module is a standard library that provides various functions/ classes to configure the logging for applications and libraries.  

import logging
Import Logging Module
Import Logging Module

2. Configure Logging 

Next, we will write a configuration file to set the logging configuration. The content, for example, will include the loggers, handlers, formatters, logger root, console handler, etc. After we create the file with content, we can import the Python code to read the content. Firstly, to achieve the desired effect, we will import logging. config and the sys module. Then load(config file) and print the (log messages). May I show you the code line by line? That would be easier to understand.

import logging.config
import sys

logging.config.fileConfig('logging.conf')

logger = logging.getLogger(__name__)

logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
Configure Logging 1

3. Use of Loggers

Objects associated with loggers are created to log messages. They are hierarchical namespaces that assist in defining and labelling types of logs, which are either established from source or purpose. 

All the logs have names, however they are also sorted by name and sorted into a hierarchical structure similar to Python’s package namespace. As you can see the log lines are printed in the image.

4. Configuring Handlers 

Additionally, these routes share the same routes for home delivery. Among those things, ‘StreamHandler’ has the superiority of redirecting log messages to streams.

These ‘StreamHandler’ can be the ‘stdout‘ or the stderr. Content Steel offers multiple handlers, for instance, FileHandler, NullHandler, and MemoryHandler. These handlers perform the task of dispatching log messages to the specified output destination.

5. Formatter/layout configuration

You can also change the layout of the log message according to your preference using the ‘Formatter’ class. First, import the logging module from Python, then create a Formatter object with a custom format. The ‘StreamHandler’ is used to configure the log message. You can see the log messages in the output. 

import logging
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(stream_handler)

logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
Formatting Configuration

6. Define logging levels

The logging module describes several logging levels, such as DEBUG, INFO, WARNING, ERROR, and CRITICAL, to demonstrate the severity of log messages. You can specify the logging level for each logger to manage which messages are logged. Developers can specify the minimum logging level for a logger to handle which messages are seized and emitted.

These are some basic steps used to configure the logging in Python. Let’s see some advantages of configuring log messages in Python. 

Advantage of Configuring Logging In Python

  1. Debugging and Troubleshooting: Logging allows developers to capture detailed information such as error messages, warnings, and informational messages throughout the execution of their code. This is essential for diagnosing issues and understanding the behavior of the application, making logging a critical tool in the software development process.
  2. Granular Control: The ability to use different logging levels to control the verbosity of logged information is beneficial for developers. They can assign different logging levels to various parts of the application, enabling them to focus on specific areas of interest during debugging and problem-solving.
  3. Customization: Python’s logging module offers a wide range of configurable components, including loggers, handlers, formatters, and filters. This allows developers to tailor the logging setup to their specific requirements. They can define custom log message formats, specify the destinations where log messages should be sent, and use filters to selectively include or exclude log messages based on certain criteria.
  4. Flexibility: Python provides the flexibility to configure logging both programmatically and through configuration files. This adaptability is valuable throughout the application’s lifecycle, as it allows developers to modify logging settings without modifying the application code itself. By using configuration files, aspects such as the location of log files can be changed without requiring a redeployment of the application.

Summary

This article gives detailed information on how to configure logging in Python. The log messages hold some information related to the application that is executed. Some basic steps must be followed while configuring the log messages in Python. These basic steps involve importing modules, customizing log messages, and configuring handlers. Log message configuration provides developers with a wide range of advantages and gives them the power of customization. Hope you will enjoy this article. 

References

Do read the official documentation on logging in Python.