Skip to main content

A logging handler wrapper to improve logging performace by sending writes to a background thread

Project description

backgroundlog

Thread-based log handler for better performance

test License Maintenance made-with-python Code style: black Imports: isort Checked with mypy PyPI pyversions PyPI version backgroundlog PyPI status PyPI download month

Do not have your python program slowed down by your logging.

Introduction

Most of the time we log on disk, and all I/O is time consuming. By leveraging a thread, it is possible to speed up your python application considerably.

Use

Default use

import logging
from backgroundlog.handlers.thread_handler import ThreadHandler

# Setting up the logging thread handler
file_handler = logging.FileHandler('/var/log/myapp.log', mode="a", encoding="utf-8")
thread_handler = ThreadHandler(file_handler)

# Creating a new logger
bg_logger = logging.getLogger('bg_logger')
bg_logger.setLevel(logging.INFO)

# Adding the thread handler
bg_logger.addHandler(thread_handler)

# Using the logger
bg_logger.info('This is a log message')

Options

Set a queue size

from backgroundlog.handlers.thread_handler import ThreadHandler

thread_handler = ThreadHandler(file_handler, queue_size=5000)

By default, the queue size is 1000.

Set a blocking policy by logging record levels

When putting the records in the queue, it could reach the queue size. We provide a way to deal with this issue: set a blocking policy by logging record level, and in the case of a non-blocking policy, the record will be discarded and we will increment a dropped log record.

Only info, error and critical records are blocking:
from backgroundlog.handlers.thread_handler import ThreadHandler
from logging import CRITICAL, ERROR, INFO

thread_handler = ThreadHandler(file_handler, blocking_levels={INFO, ERROR, CRITICAL})
Only error and critical records are blocking
from backgroundlog.handlers.thread_handler import ThreadHandler
from logging import CRITICAL, ERROR

thread_handler = ThreadHandler(file_handler, blocking_levels={ERROR, CRITICAL})
Only critical records are blocking
from backgroundlog.handlers.thread_handler import ThreadHandler
from logging import CRITICAL

thread_handler = ThreadHandler(file_handler, blocking_levels={CRITICAL})
No records are blocking
from backgroundlog.handlers.thread_handler import ThreadHandler

thread_handler = ThreadHandler(file_handler, blocking_levels=None)

By default, the error and critical records are blocking, the rest are not.

Performance testing

We have done several local testing with different logging handlers. See the file run_performance_comparison.py for a full catalog of the performance tests we run.

All tests are 100_000 iterations of creating the same logging message, and were run with Python 3.13.5 in a Macbook Pro M1 with 16 GB of RAM:

Logging Handler Spent Time vs. Baseline
Mean Time (s) Std Dev (s)
StreamHandler 0.687 0.006 baseline
FileHandler 0.687 0.007 -0.067%
ThreadHandler (StreamHandler) 0.477 0.003 -30.646%
ThreadHandler (FileHandler) 0.475 0.001 -30.865%

As you see there is a ~30% of improvement when running the thread handler. It is not much, but in some contexts it can be useful for sure.

Dependencies

This package has no dependencies.

Python version support

Minimum version support is 3.10.

License

MIT license, but if you need any other contact me.

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

backgroundlog-0.2.3.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

backgroundlog-0.2.3-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file backgroundlog-0.2.3.tar.gz.

File metadata

  • Download URL: backgroundlog-0.2.3.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for backgroundlog-0.2.3.tar.gz
Algorithm Hash digest
SHA256 7f8a2823b4ef3941c170b254f2250c255c01a80ee80a513b9a77c989332f3ce5
MD5 caded5a835a7887536ddf3b6f055fc99
BLAKE2b-256 3bc1c06771b3f6d3593a1537e79360d0689a3b4a4cf518a9fc6dd54119039c80

See more details on using hashes here.

Provenance

The following attestation bundles were made for backgroundlog-0.2.3.tar.gz:

Publisher: publish_on_pypi.yml on diegojromerolopez/backgroundlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file backgroundlog-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: backgroundlog-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for backgroundlog-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d9d9f817a8b65d7312c10e2ef7cc0f6874d13edc1a049dbadf27feac42aeb7d5
MD5 e018673da63110b789f3c9d45fb343a4
BLAKE2b-256 50596bed76cfb4e72ed008a10014fb9750f52db3784c094a9646365db7f82be1

See more details on using hashes here.

Provenance

The following attestation bundles were made for backgroundlog-0.2.3-py3-none-any.whl:

Publisher: publish_on_pypi.yml on diegojromerolopez/backgroundlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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