Log Rate Limit

1.4.2 · active · verified Thu Apr 16

Log Rate Limit is a Python library that provides a logging filter for the standard `logging` framework, designed to suppress excessive log output. It works by rate-limiting logs based on configurable streams, preventing log floods. The current version is 1.4.2, released on January 17, 2025, and it appears to be actively maintained. It requires Python versions 3.8.1 or newer, but not Python 4.0.0 or later.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to apply `StreamRateLimitFilter` to a logger, allowing 3 messages per 10 seconds for each unique stream. It shows how the default behavior handles identical messages, how to use a custom `stream_id` to group dynamic messages, and how to explicitly disable rate-limiting for critical logs using `extra=RateLimit(stream_id=None)`.

import logging
import time
from log_rate_limit import RateLimit, StreamRateLimitFilter

# Configure basic logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(name)s:%(message)s')
logger = logging.getLogger(__name__)

# Apply the rate limit filter to the logger
# This example allows 3 logs per 10 seconds for each unique stream_id
logger.addFilter(StreamRateLimitFilter(period_sec=10, max_logs=3))

print("--- Demonstrating default rate limiting (same messages) ---")
for i in range(5):
    logger.info("Repeating message!")
    time.sleep(0.5)

print("\n--- Demonstrating custom stream_id for dynamic messages ---")
# Messages with dynamic content can be grouped by a custom stream_id
for i in range(5):
    device_id = "device_A"
    logger.warning("Error on %s: process %d failed!", device_id, i, extra=RateLimit(stream_id=f"error_on_{device_id}"))
    time.sleep(0.5)

print("\n--- Demonstrating disabling rate limiting for a specific log ---")
logger.error("CRITICAL: Something went terribly wrong, do NOT rate-limit this!", extra=RateLimit(stream_id=None))
logger.info("This info log will be rate-limited by default if repeated.")
logger.info("This info log will be rate-limited by default if repeated.")

view raw JSON →