Skip to main content

Decorators to indent-log function calls with parameter and timing

Project description

Decorators to log function calls with parameter and timing

Usage

Decorators to track down function call with indent. This tool is very handly for debugging, since the out is much cleaner than using python Stack Frames. Both logging function and indent characters are configurable

Decorate a function:

from indented_logs import log_call

@log_call()
def method1(arg1, arg2):
    return arg1 + arg2

method1(3, 4)

Output:

...CALL: method1(3, 4)
...'method1' RETURN: 7
...'method1' FINISHED in 0.000003 secs

Multiple functions:

from indented_logs import log_call

@log_call()
def get_data():
    get_parameters()
    query_db(conn="sample_conn_str")

@log_call()
def get_parameters():
    pass

@log_call()
def query_db(conn):
    return covert_data("1", "2")

@log_call()
def covert_data(a, b):
    return (int(a), int(b))

get_data()

Output:

...CALL: get_data()

......CALL: get_parameters()
......get_parameters RETURN: None
......get_parameters FINISHED in 0.000004 secs


......CALL: query_db(, conn=sample_conn_str)

.........CALL: covert_data(1, 2)
.........covert_data RETURN: (1, 2)
.........covert_data FINISHED in 0.000005 secs

......query_db RETURN: (1, 2)
......query_db FINISHED in 0.000870 secs

...get_data RETURN: None
...get_data FINISHED in 0.002771 secs

Decorate class: Using log_call_cls

from indented_logs import log_call_cls

@log_call_cls(indent="***", log_time=False, logger_func=log_with_capture)
class MyClass:
    def method1(self, arg1, arg2):
        self.method2(arg1)

    def method2(self, arg1):
        pass

obj = MyClass()
obj.method1("aaaaa", "bbbbbbb")

Ouput:

***CALL: method1(aaaaa, bbbbbbb)

******CALL: method2(aaaaa)
******method2 RETURN: None
******method2 FINISHED in 0.000005 secs

***method1 RETURN: None
***method1 FINISHED in 0.000916 secs

Customize indent characters:

from indented_logs import log_call

@log_call(indent='___')
def method1(arg1, arg2):
    return arg1 + arg2

method1(3, 4)

Turn off logtime:

from indented_logs import log_call

@log_call(indent='___', log_time=False)
def method1(arg1, arg2):
    return arg1 + arg2

method1(3, 4)

Output:

___CALL: method1(3, 4)
___'method1' RETURN: 7
___'method1' FINISHED in 0.000006 sec

Development

Conda environment

Create conda environment

conda env create -f environment.yml

If any dependency is added, remember to update conda environment file

conda env export --from-history > environment.yml

Tests

Run using pytest:

pytest -s --cov=src

Build and publish

Increase the version number in setup.py, run:

python -m build .

Publish to test Pypi:

python -m twine upload --repository testpypi dist/* --skip-existing

Publish to Pypi:

python -m twine upload --repository pypi dist/* --skip-existing

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

indented-logs-1.0.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

indented_logs-1.0.3-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file indented-logs-1.0.3.tar.gz.

File metadata

  • Download URL: indented-logs-1.0.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for indented-logs-1.0.3.tar.gz
Algorithm Hash digest
SHA256 baf59f04cd318d01c606065420303c9412e8c45a4d8bbd63a7fff47b1ebcb593
MD5 e49cd140ae229dfc83afe762796e2129
BLAKE2b-256 0c8805e8ddcba3cc195daa9077e4eb79d0bf6137ff370d3f88b3bba19fa006bb

See more details on using hashes here.

File details

Details for the file indented_logs-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: indented_logs-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for indented_logs-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 141ccbfcbcb957e08627d7564695ac81e2866a8ec0b804c65ad823d9f6475faf
MD5 b6e36a2041ac7ad68dca9e0e0f678557
BLAKE2b-256 830e2ec8eb865adee284d94e94e4d6cd0b81994cb2e1b6f3a4dc1c3bfe17a05e

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