https://github.com/stefanbschneider/structlog-round
A light-weight structlog processor to round floats for prettier logging.
https://github.com/stefanbschneider/structlog-round
float floating-point logging python python3 round structlog
Last synced: 3 months ago
JSON representation
A light-weight structlog processor to round floats for prettier logging.
- Host: GitHub
- URL: https://github.com/stefanbschneider/structlog-round
- Owner: stefanbschneider
- License: mit
- Created: 2020-07-16T19:21:12.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-07-17T06:19:16.000Z (almost 5 years ago)
- Last Synced: 2024-09-15T14:48:33.481Z (8 months ago)
- Topics: float, floating-point, logging, python, python3, round, structlog
- Language: Python
- Homepage: https://pypi.org/project/structlog-round/
- Size: 15.6 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# structlog-round
A simple and light-weight [`structlog`](https://github.com/hynek/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:
```python
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 [email protected]:stefanbschneider/structlog-round.git
python setup.py install
# dependencies for testing
pip install -e .[dev]
```## Usage
```python
import structlog
import structlog_roundstructlog.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