Skip to main content

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 -

  1. 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).

  2. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

contextful-0.0.1a0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

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

Hashes for contextful-0.0.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 0335f03f60bd3c058fab8da0abbd2aa680c752b2347620c7b4236acaec6e3fb7
MD5 a9705b53a24c6488e3861f4fa3acae8d
BLAKE2b-256 df3563488c02c0d88c3573c25e01deea310136ad3dae0e1300bed9c571cb08c8

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