Python Logging

Introduction to Python Logging

Python logging is a built-in module that allows developers to track events and record messages during the execution of a program. It provides a flexible and customizable way to log information, warnings, errors, and other messages, making it easier to understand and debug code.

Logging Levels

Python logging supports different levels of severity for messages. Each level has a specific purpose and can be used to filter the output based on the desired level of detail. The available logging levels, in increasing order of severity, are:

  • DEBUG: Detailed information, typically useful for debugging purposes.
  • INFO: General information about the program’s execution.
  • WARNING: Indication that something unexpected happened or an issue that could potentially cause problems.
  • ERROR: Indicates a more serious problem that prevents the program from functioning correctly.
  • CRITICAL: A very severe error that may lead to the termination of the program.

Logging Configuration

Before using the logging module, it is important to configure it properly. The configuration determines where the log messages will be stored and how they will be formatted. The configuration can be done programmatically or by using a configuration file.

Here is an example of programmatically configuring the logging module:

import logging

logging.basicConfig(filename='example.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

In this example, the log messages will be stored in a file named ‘example.log’. The logging level is set to ‘DEBUG’, which means that all messages of level ‘DEBUG’ and above will be recorded. The format specifies how the log messages will be formatted, including the timestamp, level, and message.

Logging Messages

Once the logging module is configured, you can start logging messages using the various logging functions provided. Here are some examples:

import logging

logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')

In this example, we use the logging functions to log messages of different levels. The messages will be recorded according to the configured logging level. For example, if the logging level is set to ‘WARNING’, only messages of level ‘WARNING’ and above will be recorded.

Logging in Different Modules

Python logging allows you to log messages from different modules and organize them based on their origin. This can be useful when working on larger projects with multiple modules.

Here is an example of logging messages from different modules:

main.py:

import logging
import module1
import module2

logging.basicConfig(filename='example.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

logging.debug('This is a debug message from main.py')
logging.info('This is an info message from main.py')
logging.warning('This is a warning message from main.py')

module1.py:

import logging

logger = logging.getLogger(__name__)

def do_something():
    logger.debug('This is a debug message from module1')
    logger.info('This is an info message from module1')
    logger.warning('This is a warning message from module1')

module2.py:

import logging

logger = logging.getLogger(__name__)

def do_something_else():
    logger.debug('This is a debug message from module2')
    logger.info('This is an info message from module2')
    logger.warning('This is a warning message from module2')

In this example, the log messages from ‘main.py’, ‘module1.py’, and ‘module2.py’ will be recorded in the same log file. Each module has its own logger, which allows for better organization and separation of log messages.

Conclusion

Python logging is a powerful tool for tracking events and recording messages during program execution. It provides different levels of severity for messages, allowing developers to filter the output based on their needs. By properly configuring the logging module and using the provided logging functions, developers can easily log messages and debug their code. Logging messages from different modules is also possible, making it easier to organize and analyze log data.

Scroll to Top