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
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.
We used two versions of Python depending on if they had or not the global interpret lock:
- Python 3.15.3 (with GIL)
- Python 3.15.3t (without GIL)
All tests are 100_000 iterations of creating the same logging message, and were run in a Macbook Pro M1 with 16 GB of RAM.
Python 3.15.3 (with GIL)
| Logging Handler | Spent Time | vs. Baseline | |
|---|---|---|---|
| Mean Time (ms) | Std Dev (ms) | ||
| StreamHandler | 0.685 | 0.006 | baseline |
| FileHandler | 0.685 | 0.018 | +0.03% |
| ThreadHandler (StreamHandler) | 0.487 | 0.03 | -28.911% |
| ThreadHandler (FileHandler) | 0.475 | 0.002 | -30.66% |
There is a ~30% of improvement when running the thread handler.
Python 3.15.3t (without GIL)
| Logging Handler | Spent Time | vs. Baseline | |
|---|---|---|---|
| Mean Time (ms) | Std Dev (ms) | ||
| StreamHandler | 0.539 | 0.004 | baseline |
| FileHandler | 0.545 | 0.013 | +1.109% |
| ThreadHandler (StreamHandler) | 0.344 | 0.002 | -36.301% |
| ThreadHandler (FileHandler) | 0.339 | 0.001 | -37.118% |
There is a ~36% of improvement when running the thread handler. +6% with respect of the Python version with GIL.
Conclusions
Not blocking the main flow of your program by making use of backgroundlog gives you a 30% speed gain.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file backgroundlog-0.2.6.tar.gz.
File metadata
- Download URL: backgroundlog-0.2.6.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c6073600df61219961e4d3f85d48e352d0f87b4e8943520ecffe7cf0682c5f1
|
|
| MD5 |
7420b92544645b846e60190373089893
|
|
| BLAKE2b-256 |
48bc7bc7febc28732858e876f4727ada003b14251ba7f54eb98a07292fe7fc9a
|
Provenance
The following attestation bundles were made for backgroundlog-0.2.6.tar.gz:
Publisher:
publish_on_pypi.yml on diegojromerolopez/backgroundlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
backgroundlog-0.2.6.tar.gz -
Subject digest:
3c6073600df61219961e4d3f85d48e352d0f87b4e8943520ecffe7cf0682c5f1 - Sigstore transparency entry: 601354481
- Sigstore integration time:
-
Permalink:
diegojromerolopez/backgroundlog@f18cd2afab3d63f853bdc632014fe448cadabaa9 -
Branch / Tag:
refs/tags/0.2.6 - Owner: https://github.com/diegojromerolopez
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_on_pypi.yml@f18cd2afab3d63f853bdc632014fe448cadabaa9 -
Trigger Event:
release
-
Statement type:
File details
Details for the file backgroundlog-0.2.6-py3-none-any.whl.
File metadata
- Download URL: backgroundlog-0.2.6-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ee267f0f07669d38fe16d4a2ace7ad25b34f9ae1fb2d998264a92fbca663953
|
|
| MD5 |
d0d47f4e2a73ed87a5727e9fb26c85b2
|
|
| BLAKE2b-256 |
60b53a3ac0483ef1749ddd06770bddba54d816802770b414d36ca0be4ec6563e
|
Provenance
The following attestation bundles were made for backgroundlog-0.2.6-py3-none-any.whl:
Publisher:
publish_on_pypi.yml on diegojromerolopez/backgroundlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
backgroundlog-0.2.6-py3-none-any.whl -
Subject digest:
8ee267f0f07669d38fe16d4a2ace7ad25b34f9ae1fb2d998264a92fbca663953 - Sigstore transparency entry: 601354484
- Sigstore integration time:
-
Permalink:
diegojromerolopez/backgroundlog@f18cd2afab3d63f853bdc632014fe448cadabaa9 -
Branch / Tag:
refs/tags/0.2.6 - Owner: https://github.com/diegojromerolopez
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_on_pypi.yml@f18cd2afab3d63f853bdc632014fe448cadabaa9 -
Trigger Event:
release
-
Statement type: