Skip to main content

A package for throttling duplicate log messages in loguru

Project description

Loguru Throttler

PyPI version Python Versions License Build Status Downloads

A Python package that provides message throttling capabilities for the loguru logging library.

Table of Contents

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:

  1. Core Features Demo (throttler_demo.py): Demonstrates all core features with clear examples
  2. API Client Simulation (advanced_example.py): A realistic use case with API rate limiting
  3. Custom Throttling Strategy (custom_throttling.py): Advanced customization with level-based rules
  4. 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:

  1. Intercepting log messages
  2. Computing a hash based on message content, log level, and optionally extra fields
  3. Checking if an identical message was logged within the throttle window
  4. Applying burst mode rules if configured
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

loguru_throttler-0.2.1.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

loguru_throttler-0.2.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file loguru_throttler-0.2.1.tar.gz.

File metadata

  • Download URL: loguru_throttler-0.2.1.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for loguru_throttler-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a3d2cef14f2fc9986f7e12b3b2b22dd548d51c5a72d37add0cb578f03cd4b317
MD5 ce6962510123cf7d926d2b54379c2ed4
BLAKE2b-256 1c81d2736410ab6fb290034ce8930d34abe5e877d6b179ba89d174a1b9b777a9

See more details on using hashes here.

File details

Details for the file loguru_throttler-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for loguru_throttler-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c23ddec131b73023dac42739fcb8c6b60b650872d92bffceb70fec7d8edbbf9e
MD5 71690951fe64bff29f8c0e3601d502db
BLAKE2b-256 0d8efda935e9ee539c8200e0a507d2d16ce3250e1442977cc63a6291773fc78f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page