Skip to main content

A high-performance, buffered, non-blocking logger for Python, implemented in Rust.

Project description

High-Performance Python Logger (in Rust)

PyPI version Py Versions

A high-performance, buffered, non-blocking logger for Python, with the core logic implemented in Rust for maximum speed and efficiency.

This logger is designed for high-throughput applications where standard Python logging would become a bottleneck. It sends logs on a dedicated background thread, ensuring your application's main threads are never blocked by I/O.

Features

  • Non-Blocking: Log calls return instantly.
  • Batching: Logs are sent to destinations in efficient batches (Elastic bulk API).
  • Resilient: In-memory buffer with retries for when destinations are temporarily unavailable.
  • Multiple Outputs: Configure logging to stdout, files, and Elasticsearch simultaneously.
  • Python logging Integration: Includes a logging.Handler to seamlessly integrate with the standard library.

Requirements

  • Python 3.9+
  • pip version 21 or newer.

This package uses modern Python packaging standards (PEP 517). Older versions of pip may not be able to install it correctly. You can upgrade pip with the following command:

pip install --upgrade pip

Pre-compiled wheels for officially Supported Platforms

  • Linux: x86_64 (manylinux compatible)
  • macOS: arm64 (Apple Silicon)

Installation

pip install py-hpl-logger

Quick Start

1(Optional). Configure your logger with ElasticConfig if needed:

ELASTIC_HOST=localhost
ELASTIC_PORT=9200
ELASTIC_USERNAME=elastic
ELASTIC_PASSWORD=changeme
ELASTIC_INDEX=my-python-logs

Configure using code:

elastic_config = ElasticConfig(
    host="localhost",
    port=9200,
    index="my-app",
    username="elastic",
    password="changeme"
)

Configure using .env. You can choose .env search depth using local_only argument:

elastic_config = ElasticConfig.from_env(local_only=False)
  1. Use the logger in your Python application:

    import logging
    from py_hpl_logger import LoggerBuilder, ElasticConfig, RustLogHandler
    
    # 1. Build the Rust logger backend once
    elastic_config = ElasticConfig(
        host="localhost",
        port=9200,
        index="my-app",
        username="elastic",
        password="changeme"
    )
    # default buffer params:
    # channel_size: 4096
    # batch_size: 256
    # `channel_size` sets maximum inner buffer size. If log amount exceeds this argument, over limiting logs are dropped to avoid memory leaks
    # `batch_size` is an argument used to bulk push amount of logs specified
    rust_backend = (
        LoggerBuilder()
        .with_stdout(True) # whether to use stdout or not
        .with_file_output("my_app_session")
        .with_elastic_output(elastic_config)
        .with_batch_size(1000) # how many log rows to wait before forced flush
        .with_flush_interval(1.0) # # how many seconds to wait before forced flush
        .build()
    )
    
    # 2. Integrate with Python's standard logging
    handler = RustLogHandler(rust_logger=rust_backend)
    formatter = logging.Formatter("%(threadName)s - %(message)s")
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    
    # 3. Use the standard logging API anywhere!
    log = logging.getLogger(__name__)
    log.info("This log is being handled by Rust!")
    log.error("This is a high-performance error log.")
    
    # 4. For graceful shutdown, flush the logger before exiting
    # (The logger also attempts to flush automatically on exit)
    rust_backend.flush()
    

Benchmark

Measurements completed under python3.11 on Apple M2 Max using remote Elastic Search instance with 2 Gb memory limit. The logger version used is py-hpl-logger==0.1.5.

20.000 messages were successfully written in a total time of 1.9494 seconds

    --- Running Benchmark for: High-Performance Rust Logger ---
    Logging 20,000 messages across 10 threads...
    Time taken for High-Performance Rust Logger: 0.1023 seconds

    Flushing Rust logger buffer...
    Flush took an additional 1.8471 seconds.

    --- Benchmark Results ---
    The application code was blocked for 0.1023 seconds with the Rust logger.
    Throughput: 195,474.04 logs/second
    Logs written: 20,000
    Time taken for flush: 1.8471 seconds
    Total time: 1.9494 seconds

Changelog

[0.1.5] - 2025-11-04

  • Added 'with_channel_size' constructor to LoggerBuilder

[0.1.6] - 2025-11-04

  • Added benchmark results

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

py_hpl_logger-0.1.6-cp39-abi3-win_amd64.whl (1.7 MB view details)

Uploaded CPython 3.9+Windows x86-64

py_hpl_logger-0.1.6-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

py_hpl_logger-0.1.6-cp39-abi3-macosx_11_0_arm64.whl (1.9 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

py_hpl_logger-0.1.6-cp39-abi3-macosx_10_12_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file py_hpl_logger-0.1.6-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for py_hpl_logger-0.1.6-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 594f4ecd27ad8f77ded11979b9ae0a64c298be6f0bb799e4044e21d29e3a3e09
MD5 47f1516b2e0668c68899c4d301236081
BLAKE2b-256 8ef5253a812edde5d432149733fae9f43b633bcd14b3091ed725ec17b2491258

See more details on using hashes here.

File details

Details for the file py_hpl_logger-0.1.6-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for py_hpl_logger-0.1.6-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 836f7785c30775e3e2eed45c131bcf51c6d2012153c1a4c0462c4713ed92c9e8
MD5 be4dd28c4d6f2afa94abf4d80e36dfa7
BLAKE2b-256 8d7dedd3f56fefd8c8d8c2cce622b4ec07983ae02134735b1d3d290ee50f079a

See more details on using hashes here.

File details

Details for the file py_hpl_logger-0.1.6-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for py_hpl_logger-0.1.6-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6fe54e7cbaeb631cd4a24033ee8b1656fae38a701e2053ddd05fd761f6ef69ec
MD5 ac8601965b50db6d0bed9320c5ab769c
BLAKE2b-256 c57ff3b3db4f1cd1c5b6f87553d54c5ba96e410382ea5bf7dc7ddb03b6c850ad

See more details on using hashes here.

File details

Details for the file py_hpl_logger-0.1.6-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for py_hpl_logger-0.1.6-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c00d6c8d2a7f36ddfedf7012a11c448558c48ba9c99cba3e5a034567ea624ba0
MD5 b9e4bd203b2bd282039c22ba70d4ab03
BLAKE2b-256 bdbe27d7b55a61e0c6f5a00308cd9d9feb09beb5757ab5b14cba36f231ace25f

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