A light-weight structlog processor to round floats for prettier logging.
Project description
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 toonly_fields: A list of only fields that should be roundednot_fields: A list of fields that should not be roundednp_array_to_list(bool): Whether to castnumpyarrays to lists and round floats for prettier logging
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-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa290dc5fd96f2b1f704a09f14758fb7ddb901e78bae2b8420968fec8cf85416
|
|
| MD5 |
5594f0d8423f1678e4bb3517aaae9522
|
|
| BLAKE2b-256 |
6af2f488f09e4d42cbd734d5e2c2cdcbd758684266b624916e048ab24a135b87
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
100f9d870a3f789c1b956b69fbb066586befafc05e178952045b2d990663761d
|
|
| MD5 |
9f25d2c137a711638c04d6560b28d6d1
|
|
| BLAKE2b-256 |
7b4f82de451e10791d49484de5542dbb30528bf46a43eab4ed74c17a2b247ac6
|