Show HN: TimeBasedLogger – Python Logger That Logs Based on Time
by prakashqwerty | 2025-07-24 05:09:54+00:00
What is TimeBasedLogger? TimeBasedLogger is a simple but production-grade Python logging library that changes how you manage your logs: instead of logging on every message, it lets you log only once per specified time interval. This can greatly reduce log spam in high-frequency systems, making logs more meaningful and easier to parse.

Inspiration & Acknowledgement This project was directly inspired by John Scolaro’s article, "[Log by Time, not by Count](https://johnscolaro.xyz/blog/log-by-time-not-by-count)". The blog post argues convincingly for a time-based logging approach, especially in systems that process large volumes of events rapidly. Its clear explanation and practical pseudocode examples helped shape the concept and motivation behind TimeBasedLogger. Big thanks to John Scolaro for articulating the challenges and benefits around logging strategies so effectively.

Key Features Time-Based Logging: Emits logs only at your chosen time interval (e.g., every 2 seconds), regardless of message volume.

Max Logs per Interval: Optionally set a cap—never exceed a set number of logs during each interval.

Pause/Resume: Easily pause or resume logging with built-in controls.

Async Mode: High-performance background logging supports batching for busy loops or async workflows.

Thread Safety: Optional locking for multi-threaded environments.

Custom Log Levels & Filtering: Full support for standard levels (DEBUG, INFO, WARNING, ERROR, CRITICAL) and custom filtering.

Structured Logging/Extra Fields: Add contextual data to logs for easy parsing and analysis.

Custom Formatting: Flexible message formats using {level}, {asctime}, {message}, and any additional fields.

Exception Logging: Log tracebacks with exc_info=True—no more silent error hiding.

Installation PyPI:

bash pip install timebased-logger Manual: Download and add timebased_logger.py to your project.

Quick Usage Examples python from timebased_logger import TimeBasedLogger

# Log only once per 2 seconds logger = TimeBasedLogger(interval_seconds=2) logger.log("Hello") logger.log("World") # This won't log if called within 2 seconds Log Levels python logger = TimeBasedLogger(level='WARNING') logger.info("Not logged") logger.warning("Will log") Custom Format & Structured Data python logger = TimeBasedLogger(fmt='[{level}] {user} {message}') logger.info("User action", extra={'user': 'alice'}) Exception Logging python try: 1 / 0 except ZeroDivisionError: logger.error("An error occurred", exc_info=True) When is This Useful? IoT Devices/Sensors: Prevent log flooding from events that occur every millisecond.

Web/API Servers: Summarize frequent status checks or health pings.

Background Jobs: Avoid excessive logs in loops or polling routines.

Why Use TimeBasedLogger? Traditional loggers emit messages for every event, quickly overwhelming log files in high-traffic environments. By logging on a time-interval basis, TimeBasedLogger ensures logs remain concise, relevant, and easier to analyze, all while remaining highly customizable for professional use.

Try it now and keep your Python logs meaningful—even in the noisiest systems!