Log throttling utilities for structlog.
Project description
structlog-throttling: Throttling for structlog loggers
Logging offers a trade-off between visibility and performance. A particularly high performance cost can be incurred when logging in each iteration of a loop, common hot spots in most programs. A solution to this problem is to space out the log calls such that they only happen every some time instead of on every iteration of the loop. By tweaking the time in between log calls we can move within the visibility-performance trade-off.
structlog-throttling brings this solution to structlog in the form of processors to throttle log calls based on time, or call count.
Getting started
Installation
Install structlog-throttling from PyPI:
pip install structlog-throttling
Configure
When configuring structlog, use one of the processors offered by structlog-throttling. I recommend putting the processor close to the beginning of your processor chain, to avoid processing logs that will ultimately be dropped:
import structlog
from structlog_throttling.processors import LogTimeThrottler
structlog.configure(
processors=[
# Logs with the same 'event' will only be allowed through every 5 seconds.
LogTimeThrottler("event", every_seconds=5),
...
]
)
Examples
Configure structlog to throttle logs based on log level every 5 seconds:
import structlog
from structlog_throttling.processors import LogTimeThrottler
structlog.configure(
processors=[
structlog.processors.add_log_level,
LogTimeThrottler("level", every_seconds=5),
structlog.processors.TimeStamper(fmt="iso"),
structlog.dev.ConsoleRenderer(),
],
)
See full code.
Configure structlog to log only every other loop iteration:
import structlog
from structlog_throttling.processors import LogCountThrottler
structlog.stdlib.recreate_defaults()
structlog.configure(
processors=[
LogCountThrottler("event", every=2),
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.dev.ConsoleRenderer(),
],
)
See full code.
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 structlog_throttling-1.3.0.tar.gz.
File metadata
- Download URL: structlog_throttling-1.3.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a1e4fa68ebe0ceda230c329225dd3bf9e1e420f0105543f537e45fe6a35ce3f1
|
|
| MD5 |
873be1a492c3e08a5c2b562b1293efb6
|
|
| BLAKE2b-256 |
d03fd6c38b3cdfd180a72cfcb833fc015df700706e4c8abcded1576286fa5f2b
|
File details
Details for the file structlog_throttling-1.3.0-py3-none-any.whl.
File metadata
- Download URL: structlog_throttling-1.3.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a1b8fb6a56834fc40ef804c1f5f23c038cc98daa53c6f1d80a112c165158cf8
|
|
| MD5 |
b0f67e726e78826ad9de4e39a4bc54c2
|
|
| BLAKE2b-256 |
40965a82deb1a52c3a0247e8bc67848f82bee47b96ecf53524b73d8498085d89
|