Crate rate_log

Crate rate_log 

Source
Expand description

§Rate Log

A Rust library for rate-limited logging that prevents spam by tracking message frequency and duration. This crate helps reduce log noise by detecting repeated messages and only outputting warnings when configurable limits are exceeded.

§Features

  • Count-based rate limiting: Limit by number of repeated message occurrences
  • Duration-based rate limiting: Limit by accumulated time between repeated messages
  • Unified tracking: Always tracks both count and duration for comprehensive reporting
  • Smart duration formatting: Automatically formats durations in appropriate units (ms, s, m, h)
  • Message deduplication: Automatically resets counters when different messages are logged
  • Zero-cost abstractions: Minimal runtime overhead with compile-time optimizations
  • Test-friendly: Built-in output capture for unit testing

§Quick Start

use rate_log::{RateLog, Limit};
use std::time::Duration;

// Create a rate limiter that allows up to 5 repeated messages
let mut rate_log = RateLog::new(Limit::Rate(5));

// First occurrence of any message is always printed immediately
rate_log.log("This is a new message");  // Prints: "This is a new message"

// Log the same message multiple times - no output until limit exceeded
for i in 0..7 {
    rate_log.log("This is a new message");
}
// After 5 repetitions, it will output: "Message: \"This is a new message\" repeat for 5 times in the past 10ms"

// Different message gets printed immediately and resets counter
rate_log.log("Different message");  // Prints: "Different message"

§Rate Limiting Types

§Count-based Limiting (Limit::Rate)

Tracks the number of times the same message is logged consecutively:

use rate_log::{RateLog, Limit};

let mut logger = RateLog::new(Limit::Rate(3));

logger.log("Error occurred");     // 1st occurrence - printed immediately: "Error occurred"
logger.log("Error occurred");     // 2nd occurrence - counted silently
logger.log("Error occurred");     // 3rd occurrence - counted silently
logger.log("Error occurred");     // 4th occurrence - triggers: "Message: \"Error occurred\" repeat for 3 times in the past 15ms"
logger.log("Different error");    // New message - printed immediately: "Different error"

§Duration-based Limiting (Limit::Duration)

Accumulates the time elapsed between consecutive calls with the same message:

use rate_log::{RateLog, Limit};
use std::time::Duration;
use std::thread;

let mut logger = RateLog::new(Limit::Duration(Duration::from_secs(1)));

logger.log("Periodic event");      // 1st occurrence - printed immediately: "Periodic event"
thread::sleep(Duration::from_millis(300));
logger.log("Periodic event");      // 300ms accumulated - silent
thread::sleep(Duration::from_millis(800));
logger.log("Periodic event");      // 1100ms total - triggers: "Message: \"Periodic event\" repeat for 2 times in the past 1s"

§Behavior

  • New message printing: Every new/different message is immediately printed to stdout
  • Unified tracking: Always tracks both message count and elapsed duration regardless of limit type
  • Silent repetitions: Repeated messages are counted silently until limit exceeded
  • Smart duration formatting: Automatically displays duration in appropriate units (ms, s, m, h) with whole numbers
  • Comprehensive warnings: Rate limit violations show both count and duration: “Message: "text" repeat for X times in the past Yms”
  • Counter reset: Switching to a different message resets all counters and prints the new message

§Use Cases

  • Error logging: Prevent log spam from repeated error conditions
  • Debug output: Control verbose debug message frequency
  • Performance monitoring: Rate-limit performance warnings
  • Network logging: Manage connection retry message frequency
  • System monitoring: Control repeated system state notifications

Structs§

RateLog
A rate limiting logger that tracks message frequency and duration.

Enums§

Limit
Defines the type and threshold for rate limiting.