Skip to main content

structlog-throttling: Throttling for structlog loggers

Supported Python versions from PyPI.

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(),
    ],
)

GIF of logs throttled every 5 seconds based on log level

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(),
    ],
)

GIF of logs throttled every other loop iteration

See full code.

Metadata

Release files for structlog-throttling 1.4.1

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

Source distribution (sdist)

Source distribution for structlog-throttling 1.4.1
File Size Uploaded Provenance
structlog_throttling-1.4.1.tar.gz 5.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for structlog-throttling 1.4.1
File Interpreter ABI Platform
structlog_throttling-1.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / structlog_throttling-1.4.1.tar.gz

Download URL structlog_throttling-1.4.1.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
feb28b53e5e76dcbb51b74da135e984462d4ce61db6497cd20433e01ce09eba1
BLAKE2b-256 checksum
How to use checksums
63a37e4b8569e4c71748d1904b77b8eb8cd2ea680c791396644cce921ccd1262
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / structlog_throttling-1.4.1-py3-none-any.whl

Download URL structlog_throttling-1.4.1-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
629f2bedb45ff48ebf9c6faf8332bd14585d007ec2aa97f5bc551d9ff5b7db08
BLAKE2b-256 checksum
How to use checksums
35caefa345c78d06be2e5a15dce6987115c3cb1364991116c2d75754ee12695c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for structlog-throttling 1.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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