Logging helper for contextful logging
Project description
Context Logger
Enhanced logging features emphasizing context for complex logging requirements.
Installation
Install using pip:
pip install contextful
Usage Example
Here's an example for a process of shipping a couple of items to their respective destinations.
We use logging context to tie each log to it's respective item.
import logging
from context_logger import ContextLogger
class CustomHandler(logging.Handler):
def emit(record) -> None:
sender = record.context.get('sender')
item_id = record.context.get('item_id')
print(f'[{sender} -> {item_id}] {record.msg}')
logger = ContextLogger()
logger.addHandler(CustomHandler())
def measure_item_weight(item_contents) -> float:
... # Magic here
logger.info('Item weight was measured successfully')
def determine_item_destination(item) -> str:
... # Magic here
logger.info('Determined item address')
def ship_item(item):
... # Magic here
logger.info('Shipped item successfuly')
# Ship some items
sender = 'Johnny John'
items = [Item(id='item-1', ...), Item(id='item-1', ...)]
with logger.context({'sender': sender}):
for item in items:
with logger.context({'item': item.id}):
weight = measure_item_weight(item.contents)
destination = determine_item_destination(item)
ship_item(item)
# Output:
# [Johnny John -> item-1] Item weight was measured successfully
# [Johnny John -> item-1] Determined item address
# [Johnny John -> item-1] Shipped item successfuly
# [Johnny John -> item-2] Item weight was measured successfully
# [Johnny John -> item-2] Determined item address
# [Johnny John -> item-2] Shipped item successfuly
This simple demonstration showcases two powerful benefits of using a context for logging -
-
We were able to append valuable information - the sender name - to each of the logs on every item without having to propagate down data that was unnecessary for the logic of the function itself (- we don't need the sender name to measure an item's weight, only the contents).
-
We were able to append an item id to each log without explicitly adding it to any of them, even though they were invoked in different functions.
License
This project is open sourced under MIT license, see the LICENSE file for more details.
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 Distributions
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 contextful-0.0.1a0-py3-none-any.whl.
File metadata
- Download URL: contextful-0.0.1a0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0335f03f60bd3c058fab8da0abbd2aa680c752b2347620c7b4236acaec6e3fb7
|
|
| MD5 |
a9705b53a24c6488e3861f4fa3acae8d
|
|
| BLAKE2b-256 |
df3563488c02c0d88c3573c25e01deea310136ad3dae0e1300bed9c571cb08c8
|