Skip to main content

The Python LogDecorator with exception, in JSON format

Project description

json-log-decorator

python log decorator which raise on exception, logs stdout or to loki host, captures time taken by function or method in json format

To install package

pip install json-log-decorator
  • Example usage with just decorator

NOTE: This takes file name as name while logging if no name provided

from json_log_decorator.logdecorator import LogDecorator as log
import time

@log()
def test_decorator(x,y):
    time.sleep(2)
    return x + y

test_decorator(1,2)

log's as below

{"name": "test", "levelname": "INFO", "asctime": "2021-06-22 23:32:18,564", "message": "Function Called:[test_decorator]"}
{"name": "test", "levelname": "INFO", "asctime": "2021-06-22 23:32:20,568", "message": "Function Ended:[test_decorator] timetaken:[2] Seconds"}
  • Incase of exception, it logs with error and raises the exception with adding additional field exc_info which has traceback rasie info
from json_log_decorator.logdecorator import LogDecorator as log
import time

@log()
def test_decorator(x,y):
    time.sleep(2)
    return x + y

test_decorator(1,'s')

log's as below

{"name": "test", "levelname": "INFO", "asctime": "2021-06-22 23:29:01,097", "message": "Function Called:[test_decorator]"}
{"name": "test", "levelname": "ERROR", "asctime": "2021-06-22 23:29:03,100", "message": "Exception in test_decorator", "exc_info": "Traceback (most recent call last):\n  File \"/json-log-decorator/json_log_decorator/logdecorator.py\", line 17, in wrapper\n    val = func(*args, **kwargs)\n  File \"/json-log-decorator/json_log_decorator/test.py\", line 7, in test_decorator\n    return x + y\nTypeError: unsupported operand type(s) for +: 'int' and 'str'"}
  • To give a name to logger and for using this library to add other logging levels you can do this way
from json_log_decorator.logdecorator import LogDecorator,get_logger

logger = get_logger('test-this')
log = LogDecorator(logger)

@log
def test_decorator(x,y):
    logger.warning('to hava a warning message')
    return x + y

@log
def sum(x):
    return x+2

@log
def main():
    x = sum(test_decorator(2,3))
    logger.info(x)

main()
  • Logs can directly pushed to LOKI host , in this case you need to pass loki_host as shown below

NOTE: this library only deals with version v1 loki python library

  • Incase you want to try locally use below line to bring up grafana and loki locally , make sure you open grafana using admin and admin credentials and add datasource loki with http://loki:3100 and save the data source
wget https://raw.githubusercontent.com/grafana/loki/v2.2.1/production/docker-compose.yaml -O docker-compose.yaml
docker-compose -f docker-compose.yaml up -d
from json_log_decorator.logdecorator import LogDecorator,get_logger

logger = get_logger(name='test-this',loki_host='http://localhost:3100')
log = LogDecorator(logger)

@log
def test_decorator(x,y):
    logger.warning('to hava a warning message')
    return x + y

@log
def sum(x):
    return x+2

@log
def main():
    x = sum(test_decorator(2,3))
    logger.info(x)

main()
  • Can be seen in LOKI as below

img.png

  • To set logging Level
import logging
from json_log_decorator.logdecorator import LogDecorator,get_logger

logger = get_logger('test-debug')
logger.setLevel(logging.DEBUG)
log = LogDecorator(logger)

@log
def test_decorator(x,y):
    logger.warning('just a warning test message')
    return x + y

@log
def sum(x):
    return x+2

@log
def main():
    x = sum(test_decorator(2,3))
    logger.info(x)

main()

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

json_log_decorator-2.0.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

json_log_decorator-2.0.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file json_log_decorator-2.0.0.tar.gz.

File metadata

  • Download URL: json_log_decorator-2.0.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.5

File hashes

Hashes for json_log_decorator-2.0.0.tar.gz
Algorithm Hash digest
SHA256 02c02d5e7eb43ed591b11fe882da8267b1433216a4675aac8eaeb38b4730e1fa
MD5 1846e69218578f81fcc37cf20ff6f1ec
BLAKE2b-256 029bd71005f2546d01e7bd8033b4f2b32368a3a93b6a3ed2e6bed8b37f43dd82

See more details on using hashes here.

File details

Details for the file json_log_decorator-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: json_log_decorator-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.5

File hashes

Hashes for json_log_decorator-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3de0508101b1d1947fa71f017e8cc7b58e4ca451635d2a68fc291bafa26919f7
MD5 3ae1cf06b8f22c52cd7c93892c104367
BLAKE2b-256 6ead82452f1330849464b17404f9e4b8fb79cd85b073cc5c95b41ee24a6932ed

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