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

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 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')

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 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()
  • 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-1.2.0.tar.gz (3.3 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.2.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: json_log_decorator-1.2.0.tar.gz
  • Upload date:
  • Size: 3.3 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-1.2.0.tar.gz
Algorithm Hash digest
SHA256 6db60111422de9a4a71802d50ab8f4c166ecc85e8999e9d37e8c3ce0573e7e66
MD5 4fc1b871852c1f9f831fcd7dd5eaa481
BLAKE2b-256 8881fb7c3493cb28c2acc2d2add7503d235e795af4dfc3f1cdc86d6941bfb90e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json_log_decorator-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 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-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0013456986760e1f6d14efb617c8f42673accec81b9ca30d6d4fabae91c12f97
MD5 192669f0cb0deb1041c7edca385f2693
BLAKE2b-256 843a05902393cfcc5c55af9e22a1ec5a53e9b33613b6c83b4fe34a29309a6081

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