Skip to main content

Dynamic Logger Class for logging

Project description

DynamicLogger

DynamicLogger is an extension of logging.Logger to provide additional functionality to log "extra" values quickly and dynamically.

Installation

One Liner

python -m pip install dynamic_logger

Or if you prefer Manual Installation

git clone https://github.com/ajatkj/dynamic_logger.git
cd dynamic_logger
pip install .

Usage

import dynamic_logger
import logging
logging.setLoggerClass(dynamic_logger.Logger)

# Set-up log formatter with user-definied attributes
fmt = '[%(asctime)s] <%(app)s> [%(levelname)s] <%(id)s> <%(customer_id)s> --- %(message)s' # Note: format attributes in <> are user-definited

# Load config
logging.basicConfig(format=fmt, datefmt='%d-%b-%y %H:%M:%S', level='INFO')

applogger = logging.getLogger(__name__)

@applogger.log_extras('id',int=0,customer_id='obj.customer_id') # Log value of 'id' and 'obj.customer_id'
def example_1(id=0,id2=0,obj=None):
    applogger.info('This example shows how to log values from function arguments')

if __name__ = "__main__":
    example_1(id=123456,obj={"customer_id":777})

Above will produce log output as below for all logging calls from the function:

[2022-04-19 12:53:51,658] [INFO] <123456> <customer_id:777> --- This example shows how to log values from function arguments

dynamic_logger exposes 2 main API's

  1. log_extras decorator
    log_extras() will log values dynamically from the decorated function
  2. set_extras function
    set_extras() will log static values for all subsequent logger calls till you call the clear() function.

See more examples in example.py

Some Notes

  1. To use with FastAPI, make sure log_extras() decorator is added after the path decorator of FastAPI.

Contribution

Always open to PRs :)

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

dynamic_logger-1.0.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

dynamic_logger-1.0.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file dynamic_logger-1.0.0.tar.gz.

File metadata

  • Download URL: dynamic_logger-1.0.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Darwin/19.6.0

File hashes

Hashes for dynamic_logger-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bdfa0c9373e152d9b8a8790c78f83a751fa6344dc3b84a5c8b60cc61fdedebf5
MD5 71e6c5f4201eeba756b7659b35478b0a
BLAKE2b-256 beb4f028e7a9137050d6c5813d42bab82a7f1266e8df6f7e9059618f5da88a7e

See more details on using hashes here.

File details

Details for the file dynamic_logger-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: dynamic_logger-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Darwin/19.6.0

File hashes

Hashes for dynamic_logger-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6bce6ac6f7d81101a2167768caaf63f1a76f98c2c692c22428592ad949e1d963
MD5 95c7fb0bda9e06d66f1b8cac837883e6
BLAKE2b-256 d7a3fc0bcd9bab6aa5a61ec05aa4136ce1d0dd960f5e9e45251fdd6d4de9a76a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page