Reusable Python Code Modules, Part 9 - Logging
Effective Debugging: Implementing Reusable Logging Modules
Logging is crucial for tracking application behavior, diagnosing issues, and monitoring performance. Effective logging helps developers maintain and debug applications more efficiently. This guide covers how to structure reusable logging modules in Python using Flask, manage logs, and integrate common logging libraries.
Common Libraries and Tools
1. Python's Built-in Logging Module
Python's built-in logging
module is a flexible framework for emitting log messages from Python programs
Key Features
Built-in Library: No additional installation required
Configurable Log Levels: Supports different log levels like DEBUG, INFO, WARNING, ERROR, and CRITICAL
Multiple Handlers: Supports multiple handlers, including StreamHandler, FileHandler, and more
Flexible Configuration: Allows configuration via code or configuration files
2. Loguru
Loguru is a modern library that simplifies logging in Python, providing a more intuitive API than the built-in logging module
Key Features
Simplified API: Provides an easy-to-use interface for logging
Automatic Configuration: Automatically sets up logging configuration
Structured Logging: Supports structured logging with context information
Async Support: Works well with asynchronous code
3. Structlog
Structlog makes logging in Python more powerful by enabling structured logging
Key Features
Structured Logging: Captures log messages in a structured format, making them easier to parse and analyze
Flexible Configuration: Highly customizable, supporting different output formats and processors
Compatibility: Works well with Python's built-in logging module
Contextual Logging: Allows adding contextual information to log messages
4. Sentry
Sentry is an error tracking and performance monitoring tool that also provides comprehensive logging capabilities
Key Features
Real-Time Error Tracking: Captures and reports errors in real-time
Performance Monitoring: Monitors application performance and identifies bottlenecks
Contextual Information and Breadcrumbs: Provides detailed context for each error
Integration with Various Platforms: Supports integration with many platforms and frameworks
Comparison
Python's Built-in Logging Module: Best for basic logging needs with flexible configuration
Loguru: Ideal for developers looking for a modern, easy-to-use logging library with automatic configuration
Structlog: Suitable for applications needing structured logging and contextual information
Sentry: Best for comprehensive error tracking and performance monitoring with extensive integration capabilities
Examples
Example 1: Python's Built-in Logging Module
Setup: No additional setup required
Configuration:
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("app.log"),
logging.StreamHandler()
])
logger = logging.getLogger(__name__)
Usage:
def log_example():
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
logger.critical('Critical message')
log_example()
Example 2: Loguru
Setup:
$ pip install loguru
Configuration:
from loguru import logger
logger.add("file_{time}.log", rotation="500 MB")
Usage:
def log_example():
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
logger.critical('Critical message')
log_example()
Example 3: Structlog
Setup:
$ pip install structlog
Configuration:
import logging
import structlog
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.INFO)
structlog.configure(logger_factory=structlog.stdlib.LoggerFactory())
log = structlog.get_logger()
Usage:
def log_example():
log.debug("event", foo="bar", event="debug event")
log.info("event", foo="bar", event="info event")
log.warning("event", foo="bar", event="warning event")
log.error("event", foo="bar", event="error event")
log.critical("event", foo="bar", event="critical event")
log_example()
Example 4: Sentry
Setup:
$ pip install sentry-sdk
Configuration:
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
sentry_logging = LoggingIntegration(
level=logging.INFO, # Capture info and above as breadcrumbs
event_level=logging.ERROR # Send errors as events
)
sentry_sdk.init(
dsn="your_sentry_dsn",
integrations=[sentry_logging]
)
Usage:
def log_example():
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
logger.critical('Critical message')
log_example()