Skip to main content

Python CI

structlog-round

A simple and light-weight structlog processor to round floats for prettier logging.

Are you using structlog for convenient, structured logging in your Python program? Logging floats easily bloats your logs through many floating point digits? structlog-round rounds your floats for prettier logging but lets you keep full float precision inside your program.

For example:

log.msg("Hello world", a=1/3, b=2/3, ab_list=[1/3, 2/3])

# without structlog-round: prints long and ugly floats
# 2020-07-16 21:48.21 Hello world        a=0.3333333333333333 b=0.6666666666666666 ab_list=[0.3333333333333333, 0.6666666666666666]

# with structlog-round: floats are logged nicely rounded
# 2020-07-16 21:48.21 Hello world        a=0.333 b=0.667 ab_list=[0.333, 0.667]

structlog-round lets you configure how floats are rounded and also supports rounding floats in (nested) lists, dicts, or numpy arrays.

Install

pip install structlog-round

Or for development:

git clone git@github.com:stefanbschneider/structlog-round.git
python setup.py install
# dependencies for testing
pip install -e .[dev]

Usage

import structlog
import structlog_round

structlog.configure(
    processors=[
        # importing and adding FloatRounder to your list of processors is all you have to do
        structlog_round.FloatRounder(digits=3),
        structlog.dev.ConsoleRenderer()
    ]
)
log = structlog.get_logger()

a = 1/3
b = 2/3
log.msg("Hello world", a=a, b=b)
# this log is easily readable with short, rounded floats
# Hello world                    a=0.333 b=0.667
print(a, b)
# the floats are still available in full precision and unrounded
# 0.3333333333333333 0.6666666666666666

FloatRounder has the following configuration options:

  • digits: The number of digits to round to
  • only_fields: A list of only fields that should be rounded
  • not_fields: A list of fields that should not be rounded
  • np_array_to_list (bool): Whether to cast numpy arrays to lists and round floats for prettier logging

Metadata

Release files for structlog-round 1.0

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-round 1.0
File Size Uploaded
structlog-round-1.0.tar.gz 3.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for structlog-round 1.0
File Interpreter ABI Platform
structlog_round-1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.8 kB

Release files / structlog-round-1.0.tar.gz

Download URL structlog-round-1.0.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
aa290dc5fd96f2b1f704a09f14758fb7ddb901e78bae2b8420968fec8cf85416
BLAKE2b-256 checksum
How to use checksums
6af2f488f09e4d42cbd734d5e2c2cdcbd758684266b624916e048ab24a135b87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

Release files / structlog_round-1.0-py3-none-any.whl

Download URL structlog_round-1.0-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
100f9d870a3f789c1b956b69fbb066586befafc05e178952045b2d990663761d
BLAKE2b-256 checksum
How to use checksums
7b4f82de451e10791d49484de5542dbb30528bf46a43eab4ed74c17a2b247ac6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files

0.1

2 release files

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