Skip to main content

The Python LogDecorator with exception, in JSON format

Project description

json-log-decorator

python log decorator with info and exception, captures time taken by function or method and stdout 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

from logdecorator import LogDecorator as log
import time

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

test_decorator(1,2)

Above one gives below log

{"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
from logdecorator import LogDecorator as log
import time

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

test_decorator(1,'s')

Above one gives below log

{"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 decorator and you can add loggin messages if required
from 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()
  • To set logging Level
import logging
from 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-1.1.0.tar.gz (3.0 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-1.1.0-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

Details for the file json-log-decorator-1.1.0.tar.gz.

File metadata

  • Download URL: json-log-decorator-1.1.0.tar.gz
  • Upload date:
  • Size: 3.0 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.8.0

File hashes

Hashes for json-log-decorator-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f3752fba5764e59542cc7f3b0df58094545ed825333ebba358c537fd8ac9e132
MD5 ade884118ee548423c3488711ec15233
BLAKE2b-256 c0d23b9df2d9c2af03fd2ac111b5c4f1e27e1d88dd7344e5642e4fe33fff41e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json_log_decorator-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 2.9 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.8.0

File hashes

Hashes for json_log_decorator-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 397a8a28566d847702c5077b20fdc5dd58be93d5934372755865336a3321f3d7
MD5 ad2ca7cbeea11c31904b6e1a7decc7ff
BLAKE2b-256 ffd0f290f0d3dc58a370ee6fd50c543e5cd8324f17fdcce31c76540c1a3fe2ee

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