Python logging Module

The logging module in Python provides a flexible framework for emitting log messages from Python programs. It is used for tracking events that happen when some software runs. It can be used to understand the flow of a program and to diagnose issues.

Table of Contents

  1. Introduction
  2. Key Classes and Functions
    • basicConfig
    • getLogger
    • Logger
    • Handler
    • Formatter
    • Filter
  3. Logging Levels
  4. Examples
    • Basic Logging
    • Logging to a File
    • Formatting Log Messages
    • Logging Exceptions
    • Configuring Logging with a Dictionary
  5. Real-World Use Case
  6. Conclusion
  7. References

Introduction

The logging module allows you to track events that happen when some software runs. The module provides a way to configure different log handlers and a way of routing log messages to these handlers. It supports different log levels, making it easy to control what messages are emitted.

Key Classes and Functions

basicConfig

Configures the logging system.

import logging

logging.basicConfig(level=logging.DEBUG)
logging.debug('This is a debug message')

getLogger

Returns a logger with the specified name.

import logging

logger = logging.getLogger('example_logger')
logger.debug('This is a debug message')

Logger

Represents a logging object that is used to log messages.

import logging

logger = logging.getLogger('example_logger')
logger.setLevel(logging.DEBUG)
logger.debug('This is a debug message')

Handler

Represents a logging handler that sends log messages to a destination.

import logging

handler = logging.StreamHandler()
logger.addHandler(handler)

Formatter

Represents a formatter that specifies the layout of log messages.

import logging

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

Filter

Represents a filter that provides a way to determine which log records to output.

import logging

class CustomFilter(logging.Filter):
    def filter(self, record):
        return 'custom' in record.getMessage()

logger.addFilter(CustomFilter())

Logging Levels

  • DEBUG: Detailed information, typically of interest only when diagnosing problems.
  • INFO: Confirmation that things are working as expected.
  • WARNING: An indication that something unexpected happened, or indicative of some problem in the near future.
  • ERROR: Due to a more serious problem, the software has not been able to perform some function.
  • CRITICAL: A very serious error, indicating that the program itself may be unable to continue running.

Examples

Basic Logging

import logging

logging.basicConfig(level=logging.DEBUG)
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')

Output:

DEBUG:root:This is a debug message
INFO:root:This is an info message
WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical message

Logging to a File

import logging

logging.basicConfig(filename='app.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('example_logger')
logger.setLevel(logging.DEBUG)

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')

Output (in app.log):

example_logger - DEBUG - This is a debug message
example_logger - INFO - This is an info message
example_logger - WARNING - This is a warning message
example_logger - ERROR - This is an error message
example_logger - CRITICAL - This is a critical message

Formatting Log Messages

import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('example_logger')

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')

Output:

2024-07-26 12:00:00,000 - example_logger - DEBUG - This is a debug message
2024-07-26 12:00:00,001 - example_logger - INFO - This is an info message
2024-07-26 12:00:00,002 - example_logger - WARNING - This is a warning message
2024-07-26 12:00:00,003 - example_logger - ERROR - This is an error message
2024-07-26 12:00:00,004 - example_logger - CRITICAL - This is a critical message

Logging Exceptions

import logging

logging.basicConfig(level=logging.ERROR)

try:
    1 / 0
except ZeroDivisionError:
    logging.exception('An exception occurred')

Output:

ERROR:root:An exception occurred
Traceback (most recent call last):
  File "script.py", line 5, in <module>
    1 / 0
ZeroDivisionError: division by zero

Configuring Logging with a Dictionary

import logging
import logging.config

logging_config = {
    'version': 1,
    'formatters': {
        'default': {
            'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'default',
        },
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['console'],
    },
}

logging.config.dictConfig(logging_config)
logger = logging.getLogger('example_logger')

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')

Output:

2024-07-26 12:00:00,000 - example_logger - DEBUG - This is a debug message
2024-07-26 12:00:00,001 - example_logger - INFO - This is an info message
2024-07-26 12:00:00,002 - example_logger - WARNING - This is a warning message
2024-07-26 12:00:00,003 - example_logger - ERROR - This is an error message
2024-07-26 12:00:00,004 - example_logger - CRITICAL - This is a critical message

Real-World Use Case

Web Application Logging

In a web application, you might want to log HTTP requests and errors to both the console and a file.

import logging
import logging.config

logging_config = {
    'version': 1,
    'formatters': {
        'detailed': {
            'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'detailed',
        },
        'file': {
            'class': 'logging.FileHandler',
            'filename': 'webapp.log',
            'formatter': 'detailed',
        },
    },
    'loggers': {
        'webapp': {
            'level': 'DEBUG',
            'handlers': ['console', 'file'],
        },
    },
}

logging.config.dictConfig(logging_config)
logger = logging.getLogger('webapp')

logger.info('Web application started')
try:
    1 / 0
except ZeroDivisionError:
    logger.exception('An exception occurred')

Output (console and webapp.log):

2024-07-26 12:00:00,000 - webapp - INFO - Web application started
2024-07-26 12:00:00,001 - webapp - ERROR - An exception occurred
Traceback (most recent call last):
  File "webapp.py", line 20, in <module>
    1 / 0
ZeroDivisionError: division by zero

Conclusion

The logging module in Python provides a comprehensive and flexible logging system. It supports different log levels, handlers, formatters, and filters, making it suitable for a wide range of applications from simple scripts to complex systems.

References

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top