Skip to main content

Decorator for logging function arguments and return value by human-readable way

Project description

logwrap

https://travis-ci.org/python-useful-helpers/logwrap.svg?branch=master https://coveralls.io/repos/github/python-useful-helpers/logwrap/badge.svg?branch=master Documentation Status https://img.shields.io/pypi/v/logwrap.svg https://img.shields.io/pypi/pyversions/logwrap.svg https://img.shields.io/pypi/status/logwrap.svg https://img.shields.io/github/license/python-useful-helpers/logwrap.svg https://img.shields.io/badge/code%20style-black-000000.svg

logwrap is a helper for logging in human-readable format function arguments and call result on function call. Why? Because logging of *args, **kwargs become useless with project grow and you need more details in call log.

Cons:

  • Log records are not single line.

Pros:

  • Log records are not single 100500 symbols length line. (Especially actual for testing/development environments and for Kibana users).

  • Service free: job is done by this library and it’s dependencies. It works at virtualenv

  • Free software: Apache license

  • Open Source: https://github.com/python-useful-helpers/logwrap

  • PyPI packaged: https://pypi.python.org/pypi/logwrap

  • Self-documented code: docstrings with types in comments

  • Tested: see bages on top

  • Support multiple Python versions:

Python 3.5
Python 3.6
Python 3.7
PyPy3 3.5+

This package includes helpers:

  • logwrap - main helper. The same is LogWrap.

  • LogWrap - class with logwrap implementation. May be used directly.

  • pretty_repr

  • pretty_str

  • PrettyFormat

Usage

logwrap

The main decorator. Could be used as not argumented (@logwrap.logwrap) and argumented (@logwrap.logwrap()). Not argumented usage simple calls with default values for all positions.

Argumented usage with arguments from signature:

@logwrap.logwrap(
    log=logging.getLogger(__name__),  # __name__ = 'logwrap'
    log_level=logging.DEBUG,
    exc_level=logging.ERROR,
    max_indent=20,  # forwarded to the pretty_repr
    spec=None,  # use target callable function for spec
    blacklisted_names=None,  # list argument names, which should be dropped from log
    blacklisted_exceptions=None,  # Exceptions to skip in log
    log_call_args=True,  # Log call arguments before call
    log_call_args_on_exc=True,  # Log call arguments if exception happens
    log_traceback: bool = True,  # Log traceback if exception happens
    log_result_obj=True,  # Log result object
)

Usage examples:

@logwrap.logwrap()
def foo():
    pass

is equal to:

@logwrap.logwrap
def foo():
    pass

Get decorator for use without parameters:

get_logs = logwrap.logwrap()  # set required parameters via arguments

type(get_logs) == LogWrap  # All logic is implemented in LogWrap class starting from version 2.2.0

@get_logs
def foo():
    pass

Call example:

import logwrap

@logwrap.logwrap
def example_function1(
        arg1: str,
        arg2: str='arg2',
        *args,
        kwarg1: str,
        kwarg2: str='kwarg2',
        **kwargs
) -> tuple():
    return (arg1, arg2, args, kwarg1, kwarg2, kwargs)

example_function1('arg1', kwarg1='kwarg1', kwarg3='kwarg3')

This code during execution will produce log records:

Calling:
'example_function1'(
    # POSITIONAL_OR_KEYWORD:
    'arg1'=u'''arg1''',  # type: <class 'str'>
    'arg2'=u'''arg2''',  # type: <class 'str'>
    # VAR_POSITIONAL:
    'args'=(),
    # KEYWORD_ONLY:
    'kwarg1'=u'''kwarg1''',  # type: <class 'str'>
    'kwarg2'=u'''kwarg2''',  # type: <class 'str'>
    # VAR_KEYWORD:
    'kwargs'=
        dict({
            'kwarg3': u'''kwarg3''',
        }),
)
Done: 'example_function1' with result:

 tuple((
    u'''arg1''',
    u'''arg2''',
    (),
    u'''kwarg1''',
    u'''kwarg2''',
     dict({
        'kwarg3': u'''kwarg3''',
     }),
 ))

Limitations:

  • nested wrapping (@logwrap @deco2 …) is not parsed under python 2.7: functools.wraps limitation. Please set logwrap as the first level decorator.

LogWrap

Example construction and read from test:

log_call = logwrap.LogWrap()
log_call.log_level == logging.DEBUG
log_call.exc_level == logging.ERROR
log_call.max_indent == 20
log_call.blacklisted_names == []
log_call.blacklisted_exceptions == []
log_call.log_call_args == True
log_call.log_call_args_on_exc == True
log_call.log_traceback == True
log_call.log_result_obj == True

On object change, variable types is validated.

In special cases, when special processing required for parameters logging (hide or change parameters in log), it can be done by override pre_process_param and post_process_param.

See API documentation for details.

pretty_repr

This is specified helper for making human-readable repr on complex objects. Signature is self-documenting:

def pretty_repr(
    src,  # object for repr
    indent=0,  # start indent
    no_indent_start=False,  # do not indent the first level
    max_indent=20,  # maximum allowed indent level
    indent_step=4,  # step between indents
)

Limitation: Dict like objects is always marked inside {} for readability, even if it is collections.OrderedDict (standard repr as list of tuples).

pretty_str

This is specified helper for making human-readable str on complex objects. Signature is self-documenting:

def pretty_str(
    src,  # object for __str__
    indent=0,  # start indent
    no_indent_start=False,  # do not indent the first level
    max_indent=20,  # maximum allowed indent level
    indent_step=4,  # step between indents
)
Limitations:

Dict like objects is always marked inside {} for readability, even if it is collections.OrderedDict (standard repr as list of tuples).

Iterable types is not declared, only brackets is used.

String and bytes looks the same (its __str__, not __repr__).

PrettyFormat

PrettyFormat is the main formatting implementation class. pretty_repr and pretty_str uses instances of subclasses PrettyRepr and PrettyStr from this class. This class is mostly exposed for typing reasons. Object signature:

def __init__(
    self,
    max_indent=20,  # maximum allowed indent level
    indent_step=4,  # step between indents
)

Callable object (PrettyFormat instance) signature:

def __call__(
    self,
    src,  # object for repr
    indent=0,  # start indent
    no_indent_start=False  # do not indent the first level
)

Adopting your code

pretty_repr behavior could be overridden for your classes by implementing specific magic method:

def __pretty_repr__(
    self,
    parser  # PrettyFormat class instance,
    indent  # start indent,
    no_indent_start  # do not indent the first level
):
    return ...

This method will be executed instead of __repr__ on your object.

def __pretty_str__(
    self,
    parser  # PrettyFormat class instance,
    indent  # start indent,
    no_indent_start  # do not indent the first level
):
    return ...

This method will be executed instead of __str__ on your object.

Testing

The main test mechanism for the package logwrap is using tox. Available environments can be collected via tox -l

CI systems

For code checking several CI systems is used in parallel:

  1. Travis CI: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests. Also it’s publishes coverage on coveralls.

  2. coveralls: is used for coverage display.

CD system

Travis CI: is used for package delivery on PyPI.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logwrap-5.2.2.tar.gz (48.4 kB view details)

Uploaded Source

Built Distributions

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

logwrap-5.2.2-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

logwrap-5.2.2-cp37-cp37m-manylinux1_x86_64.whl (823.9 kB view details)

Uploaded CPython 3.7m

logwrap-5.2.2-cp37-cp37m-manylinux1_i686.whl (747.0 kB view details)

Uploaded CPython 3.7m

logwrap-5.2.2-cp36-cp36m-manylinux1_x86_64.whl (833.1 kB view details)

Uploaded CPython 3.6m

logwrap-5.2.2-cp36-cp36m-manylinux1_i686.whl (755.3 kB view details)

Uploaded CPython 3.6m

logwrap-5.2.2-cp35-cp35m-manylinux1_x86_64.whl (812.2 kB view details)

Uploaded CPython 3.5m

logwrap-5.2.2-cp35-cp35m-manylinux1_i686.whl (734.0 kB view details)

Uploaded CPython 3.5m

File details

Details for the file logwrap-5.2.2.tar.gz.

File metadata

  • Download URL: logwrap-5.2.2.tar.gz
  • Upload date:
  • Size: 48.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for logwrap-5.2.2.tar.gz
Algorithm Hash digest
SHA256 164cf7a807a8cf6263ca2cd61db829bd2bf58e6de523f43d3ecde190d53c42b5
MD5 36b0791491e5475514acb756ecbd33cd
BLAKE2b-256 ab72e65a9690124532078b915be16ed41ce7ac0ca1b0ffe69f7edce37effd0f2

See more details on using hashes here.

File details

Details for the file logwrap-5.2.2-py3-none-any.whl.

File metadata

  • Download URL: logwrap-5.2.2-py3-none-any.whl
  • Upload date:
  • Size: 21.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for logwrap-5.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f66082a00a351889966fd91e1a28a154bac233e430b506190f1fafc085ce9b35
MD5 898b80f4f5356f1e563f3b246028b724
BLAKE2b-256 e466d96ed43eac186cd81e6e2188284b3e1f0550f9d4aaa3e2fc48c0193a5b65

See more details on using hashes here.

File details

Details for the file logwrap-5.2.2-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: logwrap-5.2.2-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 823.9 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for logwrap-5.2.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6bdfd304d32325f0dcd4eef175a54f71dead34944a5be2772fab779329e9c026
MD5 ebe00fed515e3621b495b79e5020394c
BLAKE2b-256 9ebeb46c7be9bf873ee5cffe00cb964d1be82b5f5792465d6acc39f4423705d6

See more details on using hashes here.

File details

Details for the file logwrap-5.2.2-cp37-cp37m-manylinux1_i686.whl.

File metadata

  • Download URL: logwrap-5.2.2-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 747.0 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for logwrap-5.2.2-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 06bf3181d9a09c4d39e0349ba127b1cf04a5a9f2b600ddbb6bea6bf1cc99217f
MD5 bdd49c852dd9978c6deb8e49599511a8
BLAKE2b-256 5b09c787de3ed3099ed2c05d596e3cd0317ade73b44ca2f87b4fe5c1329d9996

See more details on using hashes here.

File details

Details for the file logwrap-5.2.2-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: logwrap-5.2.2-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 833.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for logwrap-5.2.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0d5b147b8ac830a47fecf3ddc5ed2a6d475341ddadc34f851a8d30856e02e2e6
MD5 77e027800e1b14e4f9719a16702da7d8
BLAKE2b-256 15f0c6d3a23ea46521708c303055f52e976ddc474c4400d1cb9c7d8323309c18

See more details on using hashes here.

File details

Details for the file logwrap-5.2.2-cp36-cp36m-manylinux1_i686.whl.

File metadata

  • Download URL: logwrap-5.2.2-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 755.3 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for logwrap-5.2.2-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 dcbdd66af21981bd2cb27ed6810865be2515f5e2a9a1651c715746f39ded199a
MD5 1ed96c19c9913600fa98a47aa6665bd2
BLAKE2b-256 feaf7048d4b37f7185da5e2bc54504e56e96bc5f4df5c8105bf68119e90ac7f8

See more details on using hashes here.

File details

Details for the file logwrap-5.2.2-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: logwrap-5.2.2-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 812.2 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for logwrap-5.2.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e9ef84ddb1ec92266b50935a6f37c20885985f101d8544773b2f5aabfee2d652
MD5 e93923a2c815b37ce5496408cfb1047f
BLAKE2b-256 82a80e8dc4dcf4ae4ac5f9b456bafc3ba854df75f0210b279176572c05669067

See more details on using hashes here.

File details

Details for the file logwrap-5.2.2-cp35-cp35m-manylinux1_i686.whl.

File metadata

  • Download URL: logwrap-5.2.2-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 734.0 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for logwrap-5.2.2-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 9f59f31ae4a97c5fc0be1b1a11bf58872975cb59cce08541ed533a130ba12768
MD5 6a92751367c43cdab8c6a7e9f413ed69
BLAKE2b-256 37482ac4b3c19fc468b88fe81d6e0d2c03db8379306c6b66a2a2360989458f7f

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