Skip to main content

Log throttling utilities for structlog.

Project description

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.

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

structlog_throttling-1.4.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

structlog_throttling-1.4.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file structlog_throttling-1.4.1.tar.gz.

File metadata

  • Download URL: structlog_throttling-1.4.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for structlog_throttling-1.4.1.tar.gz
Algorithm Hash digest
SHA256 feb28b53e5e76dcbb51b74da135e984462d4ce61db6497cd20433e01ce09eba1
MD5 611ecea005b19e101a7660ee8b8d6300
BLAKE2b-256 63a37e4b8569e4c71748d1904b77b8eb8cd2ea680c791396644cce921ccd1262

See more details on using hashes here.

File details

Details for the file structlog_throttling-1.4.1-py3-none-any.whl.

File metadata

  • Download URL: structlog_throttling-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for structlog_throttling-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 629f2bedb45ff48ebf9c6faf8332bd14585d007ec2aa97f5bc551d9ff5b7db08
MD5 9d926f3799550bab5e480757be5a2e31
BLAKE2b-256 35caefa345c78d06be2e5a15dce6987115c3cb1364991116c2d75754ee12695c

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