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:3100and 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
- 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()
Metadata
Release files for json-log-decorator 2.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| json_log_decorator-2.0.0.tar.gz | 3.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| json_log_decorator-2.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.3 kB
Release files / json_log_decorator-2.0.0.tar.gz
| Download URL | json_log_decorator-2.0.0.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
02c02d5e7eb43ed591b11fe882da8267b1433216a4675aac8eaeb38b4730e1fa
|
|
BLAKE2b-256 checksum How to use checksums |
029bd71005f2546d01e7bd8033b4f2b32368a3a93b6a3ed2e6bed8b37f43dd82
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / json_log_decorator-2.0.0-py3-none-any.whl
| Download URL | json_log_decorator-2.0.0-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3de0508101b1d1947fa71f017e8cc7b58e4ca451635d2a68fc291bafa26919f7
|
|
BLAKE2b-256 checksum How to use checksums |
6ead82452f1330849464b17404f9e4b8fb79cd85b073cc5c95b41ee24a6932ed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for json-log-decorator 2.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.