Skip to main content

Isolated Logging

A Python library for performance monitoring and optimization through detailed execution timing, statistics tracking, and color-coded logging.

Features

  • 🎯 Function Timing: Decorator-based automatic timing with statistics tracking
  • 🔄 Loop Performance: Monitor iterations with progress tracking and ETA calculation
  • ⚡ Parallel Processing: Built-in parallel loop execution with timing
  • 📊 Rich Statistics: Track averages, standard deviations, and execution patterns
  • 🎨 Color-Coded Output: Enhanced readability with ANSI color support
  • 📍 Checkpoint System: Named checkpoints for tracking execution milestones
  • 📝 Flexible Logging: Output to file, external logger, or stdout

Installation

From PyPI (when available)

pip install isolated-logging

From Source

git clone https://github.com/jurrutiag/isolated-logging.git
cd isolated-logging
pip install -e .

With Optional Dependencies

# For parallel processing support
pip install "isolated-logging[parallel]"

# For development
pip install "isolated-logging[testing]"

Quick Start

import time
from isolated_logging import (
    setup_log_file_and_logger,
    log_timed_function,
    log_timed_loop,
    log_message,
    log_checkpoint
)

# Initialize logging
setup_log_file_and_logger(setup_independent_logging=True)

# Time a function with automatic statistics
@log_timed_function(threshold=0.01)
def process_item(item):
    time.sleep(0.1)
    return item * 2

# Monitor loop performance with ETA
items = range(100)
for item in log_timed_loop(items, loop_name="Processing"):
    result = process_item(item)

    # Log intermediate results
    if item % 10 == 0:
        log_message(f"Processed {item} items")
        log_checkpoint(f"batch_{item//10}")

Core Features

Function Timing

@log_timed_function(
    ignore_instant_returns=True,  # Skip logging for instant returns
    threshold=0.001,              # Only log if execution > threshold
    include_args=True             # Include function arguments in logs
)
def expensive_operation(data):
    # Your code here
    pass

Loop Monitoring

# Basic loop timing with progress
for item in log_timed_loop(items, loop_name="Training"):
    process(item)

# Get iteration statistics
for i, item in enumerate(log_timed_loop(items)):
    if i % 100 == 0:
        stats = get_loop_stats("Loop")
        print(f"Average time: {stats['avg_time']:.4f}s")

Parallel Processing

from isolated_logging import log_timed_parallel_loop

def process_chunk(item):
    # CPU-intensive work
    return item ** 2

# Process in parallel with automatic timing
results = log_timed_parallel_loop(
    items,
    process_chunk,
    n_jobs=4,
    loop_name="Parallel Processing"
)

Checkpoints

log_checkpoint("data_loaded")
# ... some processing ...
log_checkpoint("model_trained")
# ... more processing ...
log_checkpoint("results_saved")

# Get checkpoint statistics
stats = get_checkpoint_stats()
print(f"Time from data_loaded to model_trained: {stats['model_trained']['time_since_last']:.2f}s")

Custom Logging

# Log with colors
from isolated_logging import log_message_with_color, Color

log_message_with_color("Success!", Color.GREEN)
log_message_with_color("Warning!", Color.YELLOW)
log_message_with_color("Error!", Color.RED)

# Use external logger
import logging
logger = logging.getLogger(__name__)
setup_log_file_and_logger(logger=logger)

Advanced Usage

Retrieving Statistics

from isolated_logging import (
    get_function_stats,
    get_loop_stats,
    get_checkpoint_stats,
    print_all_stats
)

# Get function execution statistics
func_stats = get_function_stats("expensive_operation")
print(f"Called {func_stats['count']} times")
print(f"Average time: {func_stats['avg_time']:.4f}s")

# Get loop performance data
loop_stats = get_loop_stats("Training")
print(f"Total iterations: {loop_stats['count']}")
print(f"Time per iteration: {loop_stats['avg_time']:.4f}s ± {loop_stats['std_time']:.4f}s")

# Print comprehensive statistics
print_all_stats()

Configuration Options

setup_log_file_and_logger(
    logger=custom_logger,           # Use existing logger
    log_file_path="/tmp/perf.log",  # Custom log file location
    setup_independent_logging=True,  # Create independent logger
    log_level=logging.DEBUG,        # Set logging level
    disable_colors=False            # Disable ANSI colors
)

API Reference

Setup Functions

  • setup_log_file_and_logger(): Initialize logging system
  • close_log_file(): Close log file handle

Timing Decorators & Context Managers

  • @log_timed_function(): Decorator for function timing
  • log_timed_loop(): Context manager for loop timing
  • log_timed_parallel_loop(): Parallel processing with timing

Logging Functions

  • log_message(): Log a message
  • log_message_with_color(): Log with color
  • log_checkpoint(): Create named checkpoint

Statistics Functions

  • get_function_stats(): Retrieve function statistics
  • get_loop_stats(): Retrieve loop statistics
  • get_checkpoint_stats(): Retrieve checkpoint data
  • print_all_stats(): Display all statistics

Development

Setup Development Environment

# Clone repository
git clone https://github.com/jurrutiag/isolated-logging.git
cd isolated-logging

# Install in development mode with test dependencies
pip install -e ".[testing]"

# Install pre-commit hooks
pre-commit install

Running Tests

# Run all tests with coverage
pytest --cov=isolated_logging

# Run specific test file
pytest tests/test_functional.py

# Run with verbose output
pytest -v

Code Quality

# Format code
ruff format

# Check linting
ruff check --fix

# Type checking
mypy src/isolated_logging

# Run all pre-commit hooks
pre-commit run --all-files

Building Package

# Build distribution
python -m build

# Install locally
pip install dist/isolated_logging-*.whl

Requirements

  • Python 3.10+
  • NumPy (for statistics)
  • Loky (optional, for parallel processing)

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues and questions, please use the GitHub issue tracker.

Metadata

Release files for isolated-logging 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for isolated-logging 0.1.2
File Size Uploaded
isolated_logging-0.1.2.tar.gz 34.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for isolated-logging 0.1.2
File Interpreter ABI Platform
isolated_logging-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 43.7 kB

Release files / isolated_logging-0.1.2.tar.gz

Download URL isolated_logging-0.1.2.tar.gz
Size 34.0 kB
Tags Source
SHA-256 checksum
How to use checksums
5d81d97ca1ffeca696afe7963678bb3cc7020ee8d1fb59ffc2b500bac184c712
BLAKE2b-256 checksum
How to use checksums
316dedd9ffac37819e82958eb04ef6ec8061081fbf766363f324322b1321f34d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Release files / isolated_logging-0.1.2-py3-none-any.whl

Download URL isolated_logging-0.1.2-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b033cd1353d8c38acbb695167da0717f5c4c07c6556177aab57e75df445e65dd
BLAKE2b-256 checksum
How to use checksums
3b255af79908eb21354318dc560dcf57dfe13586612ddd0cff8f0955286df228
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page