A package for throttling duplicate log messages in loguru
Project description
Loguru Throttler
A Python package that provides message throttling capabilities for the loguru logging library.
Table of Contents
- Features
- Installation
- Requirements
- Usage
- Examples
- How It Works
- Performance
- Future Enhancements
- Development
- License
Features
- Throttle duplicate log messages on a per-sink, per-level, and per-message basis
- Configurable time window for suppression
- Burst mode to allow a specified number of messages before throttling
- Option to include extra fields in throttling decisions
- Thread-safe operation
- Minimal performance overhead
- Easy integration with existing loguru sinks
Installation
pip install loguru-throttler
Requirements
- Python 3.9 or higher
- loguru
Usage
Basic Usage
from loguru import logger
from loguru_throttler import ThrottleSink
# Add a throttled sink with a 30-second window
logger.add("file.log", filter=ThrottleSink(throttle_time=30), level="INFO")
# Or use the helper function
from loguru_throttler import add_throttled_sink
add_throttled_sink(logger, "output.log", throttle_time=60, level="DEBUG")
# Now identical log messages will be throttled
for _ in range(100):
logger.info("This message will only appear once per 30 seconds")
Advanced Configuration
from loguru_throttler import ThrottleSink
# Custom throttling configuration
throttler = ThrottleSink(
throttle_time=120, # 2 minutes
max_cache_size=1000, # Limit memory usage
burst_limit=3, # Allow 3 messages before throttling
burst_window=60, # Reset burst counter after 60 seconds
include_extra=True # Include extra fields in throttling decisions
)
# Add to logger
logger.add("app.log", filter=throttler, level="INFO")
# Messages with different extra fields are treated as different messages
logger.bind(request_id="12345").info("Processing request")
logger.bind(request_id="67890").info("Processing request") # Not throttled (different request_id)
Burst Mode
Burst mode allows a specified number of messages to pass through before throttling kicks in:
from loguru_throttler import ThrottleSink
# Allow 5 messages within a 30-second window before throttling
burst_throttler = ThrottleSink(
throttle_time=30,
burst_limit=5,
burst_window=30
)
logger.add(sys.stdout, filter=burst_throttler, level="INFO")
# The first 5 identical messages will be logged, then throttling begins
for i in range(10):
logger.info("This message will appear 5 times before being throttled")
Examples
Check out the examples directory for comprehensive demonstrations:
- Core Features Demo (throttler_demo.py): Demonstrates all core features with clear examples
- API Client Simulation (advanced_example.py): A realistic use case with API rate limiting
- Custom Throttling Strategy (custom_throttling.py): Advanced customization with level-based rules
- Performance Benchmark (benchmark.py): Measure the performance impact of throttling
Run any example with:
python examples/throttler_demo.py
How It Works
The throttler works by:
- Intercepting log messages
- Computing a hash based on message content, log level, and optionally extra fields
- Checking if an identical message was logged within the throttle window
- Applying burst mode rules if configured
- Suppressing duplicate messages or forwarding unique ones to the sink
Performance
The throttling mechanism adds minimal overhead (<10%) to logging operations while effectively reducing log volume for repetitive messages. See the benchmark example for detailed performance metrics.
Future Enhancements
The following features are planned for upcoming releases:
v0.3.0 (Planned)
- Custom Throttling Keys: Allow users to provide custom functions for generating throttling keys
- Throttling Statistics: Track and report how many messages were throttled
- Throttling Notifications: Option to log a summary message when throttling occurs (e.g., "Suppressed 42 similar messages in the last 60 seconds")
v0.4.0 (Planned)
- Pattern-Based Throttling: Allow throttling based on regex patterns rather than exact message matches
- Adaptive Throttling: Automatically adjust throttle windows based on message frequency
- Sink-Specific Configuration: Support different throttling rules for different sinks
Future Considerations
- Message Aggregation: Combine similar messages into aggregated summaries
- Distributed Throttling: Support for throttling across multiple processes or servers
- Configuration via Environment Variables: Allow configuration through environment variables
Development
Setup
Clone the repository and install development dependencies:
# Install in development mode
pip install -e .
pip install -r requirements-dev.txt
Running Tests
# Using pytest directly
pytest tests/ --cov=loguru_throttler
# Or using the build tools
python build_tools.py test
Building the Package
You can use either the Makefile (Unix/Linux/macOS) or the cross-platform build script:
Using Makefile (Unix/Linux/macOS)
# Build the package
make build
# Run tests
make test
# Format code
make format
# Run linting
make lint
Using build_tools.py (Cross-platform)
# Build the package
python build_tools.py build
# Run tests
python build_tools.py test
# Format code
python build_tools.py format
# Run linting
python build_tools.py lint
Windows Development
When developing on Windows, be aware that files and directories starting with a dot (like .github or .gitignore) require special handling. A helper script is provided:
# Create or update GitHub templates and workflows
python create_github_templates.py
For more details, see the Contributing Guide.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file loguru_throttler-0.2.2.tar.gz.
File metadata
- Download URL: loguru_throttler-0.2.2.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d74668da21c11c31af7b55a02cd2b89e2524d5c76ad1bfcf4079b5459dd7a1ce
|
|
| MD5 |
f171afddb3c4752f66cc75e6f3fb13ab
|
|
| BLAKE2b-256 |
78126b5eaf89c479cf236c306029212ccf51f8853b47bf59b59ddca06419f217
|
File details
Details for the file loguru_throttler-0.2.2-py3-none-any.whl.
File metadata
- Download URL: loguru_throttler-0.2.2-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
809e1ac05e1eac40c303174c0c5832c41012cb5eff16c03757b2e58e333bf0e2
|
|
| MD5 |
257866257cb86308725f42e227f8950a
|
|
| BLAKE2b-256 |
14758fa5f72723e416001b7933a6a711a3dfb3d3fe5ebf79884c47672eb39b2b
|