Skip to main content

A logger that logs messages based on time intervals, not message count.

Project description

TimeBasedLogger

PyPI version Build Status License: MIT Python Versions

A Python logger that only logs messages at a specified time interval.


🚀 Production-Grade Features

  • Log Levels: DEBUG, INFO, WARNING, ERROR, CRITICAL, with filtering and convenience methods.
  • Log Formatting: Customizable log message format (default: [{{level}}] {{asctime}} {{message}}).
  • Exception Logging: Log exceptions with stack traces using exc_info=True.
  • Structured/Extra Data: Add extra fields to log records for structured logging.

Performance Metrics

Name (time in us) Min Max Mean StdDev Median IQR Outliers OPS (Kops/s) Rounds Iterations
test_builtin_logging 19.0160 65.4650 22.2032 2.9417 21.4510 1.9130 397;344 45.0386 5508 1
test_loguru_logging 14.8380 54.3040 16.3561 1.9979 15.9800 0.6510 396;568 61.1391 9767 1
test_structlog_logging 23.2850 61.8090 25.7771 3.4665 24.9380 1.0930 414;584 38.7941 6549 1
test_timebased_logger 7.1740 39.0150 7.8676 1.6970 7.4240 0.0900 1985;2676 127.1042 22500 1
**Legend:**


- **Outliers:** 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.  
- **OPS:** Operations Per Second, computed as 1 / Mean (displayed in Kops/s = thousands of operations per second)

Inspiration & Acknowledgement

This project was inspired by the article Log by Time, not by Count by John Scolaro. Highly recommended for anyone interested in effective logging strategies in high-frequency systems.

Project Links

Installation

Install from PyPI:

pip install timebased-logger

Or, just copy timebased_logger.py into your project.

Features

  • Log messages only once per specified interval
  • Limit the number of logs per interval (max_logs_per_interval)
  • Pause and resume logging
  • Custom time function for advanced testing
  • High performance async mode: background logging with batching
  • Thread safety: optional locking for multi-threaded use
  • Log levels, formatting, and exception logging (see below)

Usage

Basic Usage

from timebased_logger import TimeBasedLogger
logger = TimeBasedLogger(interval_seconds=2)
logger.log("Hello")
logger.log("World")  # Will not log if called within 2 seconds

Log Levels and Filtering

logger = TimeBasedLogger(level='WARNING')
logger.info("This will NOT be logged")
logger.warning("This will be logged")
logger.error("This will also be logged")

Convenience Methods

logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")
logger.critical("Critical message")

Log Formatting

logger = TimeBasedLogger(fmt='[{level}] {asctime} {message}')
logger.info("Formatted log message")

Exception Logging

try:
    1/0
except ZeroDivisionError:
    logger.error("An error occurred", exc_info=True)

Structured/Extra Data

logger = TimeBasedLogger(fmt='[{level}] {user} {message}')
logger.info("User logged in", extra={'user': 'alice'})

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

timebased_logger-0.1.5.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

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

timebased_logger-0.1.5-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file timebased_logger-0.1.5.tar.gz.

File metadata

  • Download URL: timebased_logger-0.1.5.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for timebased_logger-0.1.5.tar.gz
Algorithm Hash digest
SHA256 9e3432e1726b10b87384ace1b9f217c7c046c1c147b21c5c9059a9d7da49984f
MD5 ea1725402754a4912673dc164c72a823
BLAKE2b-256 dfee284157e951d49cc3be94d16be1a7265f63687c27b0e9acd98efd5097bc87

See more details on using hashes here.

File details

Details for the file timebased_logger-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for timebased_logger-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ae8f7dfd195e49bd35eb41851d1b5b0a2105a98082345986ce4deab6050dd8e6
MD5 d2fea072cf4cac74ed8283904615c730
BLAKE2b-256 e0beb2a891deef313b7809ccdda164e34be715edf7fdcd2a51af16a2860e41e7

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