Skip to main content

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

Project description

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

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-round-1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

structlog_round-1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file structlog-round-1.0.tar.gz.

File metadata

  • Download URL: structlog-round-1.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for structlog-round-1.0.tar.gz
Algorithm Hash digest
SHA256 aa290dc5fd96f2b1f704a09f14758fb7ddb901e78bae2b8420968fec8cf85416
MD5 5594f0d8423f1678e4bb3517aaae9522
BLAKE2b-256 6af2f488f09e4d42cbd734d5e2c2cdcbd758684266b624916e048ab24a135b87

See more details on using hashes here.

File details

Details for the file structlog_round-1.0-py3-none-any.whl.

File metadata

  • Download URL: structlog_round-1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for structlog_round-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 100f9d870a3f789c1b956b69fbb066586befafc05e178952045b2d990663761d
MD5 9f25d2c137a711638c04d6560b28d6d1
BLAKE2b-256 7b4f82de451e10791d49484de5542dbb30528bf46a43eab4ed74c17a2b247ac6

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