Skip to main content

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

Project description

logwrap

https://travis-ci.com/python-useful-helpers/logwrap.svg?branch=master Azure DevOps builds 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.6
Python 3.7
Python 3.8

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

  • LogOnAccess - property with logging on successful get/set/delete or failure.

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=None,  # if not set: try to find LOGGER, LOG, logger or log object in target module and use it if it logger instance. Fallback: logger named 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 details in log (no traceback, no exception details - just class name)
    log_call_args=True,  # Log call arguments before call
    log_call_args_on_exc=True,  # Log call arguments if exception happens
    log_traceback = 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 (python 3.8):

import logwrap

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

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

This code during execution will produce log records:

Calling:
'example_function1'(
    # POSITIONAL_ONLY:
    arg0='arg0',  # type: str
    # POSITIONAL_OR_KEYWORD:
    arg1='arg1',  # type: str
    arg2='arg2',  # type: str
    # VAR_POSITIONAL:
    args=(),
    # KEYWORD_ONLY:
    kwarg1='kwarg1',  # type: str
    kwarg2='kwarg2',  # type: str
    # VAR_KEYWORD:
    kwargs={
        'kwarg3': 'kwarg3',
    },
)
Done: 'example_function1' with result:

 (
    'arg0',
    'arg1',
    'arg2',
    (),
    'kwarg1',
    'kwarg2',
    {
        'kwarg3': 'kwarg3',
    },
 )

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
)

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.

LogOnAccess

This special case of property is useful in cases, where a lot of properties should be logged by similar way without writing a lot of code.

Basic API is conform with property, but in addition it is possible to customize logger, log levels and log conditions.

Usage examples:

  1. Simple usage. All by default. logger is re-used:

    • from instance if available with names logger or log,

    • from instance module if available with names LOGGER, log,

    • else used internal logwrap.log_on_access logger.

import logging

class Target(object):

    def init(self, val='ok')
        self.val = val
        self.logger = logging.get_logger(self.__class__.__name__)  # Single for class, follow subclassing

    def __repr__(self):
        return "{cls}(val={self.val})".format(cls=self.__class__.__name__, self=self)

    @logwrap.LogOnAccess
    def ok(self):
        return self.val

    @ok.setter
    def ok(self, val):
        self.val = val

    @ok.deleter
    def ok(self):
        self.val = ""
  1. Use with global logger for class:

class Target(object):

  def init(self, val='ok')
      self.val = val

  def __repr__(self):
      return "{cls}(val={self.val})".format(cls=self.__class__.__name__, self=self)

  @logwrap.LogOnAccess
  def ok(self):
      return self.val

  @ok.setter
  def ok(self, val):
      self.val = val

  @ok.deleter
  def ok(self):
      self.val = ""

  ok.logger = 'test_logger'
  ok.log_level = logging.INFO
  ok.exc_level = logging.ERROR
  ok.log_object_repr = True  # As by default
  ok.log_before = True  # As by default
  ok.log_success = True  # As by default
  ok.log_failure = True  # As by default
  ok.log_traceback = True  # As by default
  ok.override_name = None  # As by default: use original name

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.

  3. Azure CI: is used for functional tests on Windows.

CD systems

  1. Travis CI: is used for linux and SDIST 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-8.0.0.tar.gz (42.5 kB view details)

Uploaded Source

Built Distributions

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

logwrap-8.0.0-py3-none-any.whl (26.2 kB view details)

Uploaded Python 3

logwrap-8.0.0-cp38-cp38m-win_amd64.whl (189.9 kB view details)

Uploaded CPython 3.8mWindows x86-64

logwrap-8.0.0-cp38-cp38m-win32.whl (164.4 kB view details)

Uploaded CPython 3.8mWindows x86

logwrap-8.0.0-cp38-cp38-manylinux1_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.8

logwrap-8.0.0-cp38-cp38-manylinux1_i686.whl (1.1 MB view details)

Uploaded CPython 3.8

logwrap-8.0.0-cp37-cp37m-win_amd64.whl (182.0 kB view details)

Uploaded CPython 3.7mWindows x86-64

logwrap-8.0.0-cp37-cp37m-win32.whl (158.6 kB view details)

Uploaded CPython 3.7mWindows x86

logwrap-8.0.0-cp37-cp37m-manylinux1_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.7m

logwrap-8.0.0-cp37-cp37m-manylinux1_i686.whl (1.0 MB view details)

Uploaded CPython 3.7m

logwrap-8.0.0-cp36-cp36m-win_amd64.whl (182.0 kB view details)

Uploaded CPython 3.6mWindows x86-64

logwrap-8.0.0-cp36-cp36m-win32.whl (158.7 kB view details)

Uploaded CPython 3.6mWindows x86

logwrap-8.0.0-cp36-cp36m-manylinux1_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.6m

logwrap-8.0.0-cp36-cp36m-manylinux1_i686.whl (1.0 MB view details)

Uploaded CPython 3.6m

File details

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

File metadata

  • Download URL: logwrap-8.0.0.tar.gz
  • Upload date:
  • Size: 42.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1

File hashes

Hashes for logwrap-8.0.0.tar.gz
Algorithm Hash digest
SHA256 1f9c728631e1cd575554f8c819af503446162beb12e66eb4c9e7dc2bdf948552
MD5 2d81b305cfc14c85228e549247621819
BLAKE2b-256 87e2d1a7953f894d4b9ab69f63d559417c677b348dd97bc1885f62ce32df8a70

See more details on using hashes here.

File details

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

File metadata

  • Download URL: logwrap-8.0.0-py3-none-any.whl
  • Upload date:
  • Size: 26.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1

File hashes

Hashes for logwrap-8.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c52adaf58bb4d91814387a2dc1873bfef960f7197bc16ef495a4c64b904b4461
MD5 84ee5c27814254501b31c7d552b860a9
BLAKE2b-256 546d3d24706cbe4a47707331441b0e1b9a9d28de169cfd7da0bbfbd588d1dbba

See more details on using hashes here.

File details

Details for the file logwrap-8.0.0-cp38-cp38m-win_amd64.whl.

File metadata

  • Download URL: logwrap-8.0.0-cp38-cp38m-win_amd64.whl
  • Upload date:
  • Size: 189.9 kB
  • Tags: CPython 3.8m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.0

File hashes

Hashes for logwrap-8.0.0-cp38-cp38m-win_amd64.whl
Algorithm Hash digest
SHA256 39337fe5de93c2c8bfb5e6773282443c89ddaeb6a7661d9695ac24c54661bd9e
MD5 9ce79f9ffd65651712f3cf823c4d1f15
BLAKE2b-256 9ed07bc4b127650373846508de1298183a0c29e6d457987790e72dc6456b3b45

See more details on using hashes here.

File details

Details for the file logwrap-8.0.0-cp38-cp38m-win32.whl.

File metadata

  • Download URL: logwrap-8.0.0-cp38-cp38m-win32.whl
  • Upload date:
  • Size: 164.4 kB
  • Tags: CPython 3.8m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.0

File hashes

Hashes for logwrap-8.0.0-cp38-cp38m-win32.whl
Algorithm Hash digest
SHA256 11082fef22446473a000fb75a707a8f20f47591eede809d6ff9d98d4c56ec392
MD5 71de7005eeca539f0d3c7dc83b5a9311
BLAKE2b-256 3fa63cf06b55fbaea94994a64476591c0c9dde83327fb8d312391b68a037ff30

See more details on using hashes here.

File details

Details for the file logwrap-8.0.0-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: logwrap-8.0.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1

File hashes

Hashes for logwrap-8.0.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1c73229c4bafabdd49ff06b0ac0e14b3c099ce14a62d9f4429a88983803972e1
MD5 30a08005be19c1adf150310b087c9a22
BLAKE2b-256 a0bd640235b57a6cea4a75870142b8d138fef308b66c3fb509f180e7c08cdc4a

See more details on using hashes here.

File details

Details for the file logwrap-8.0.0-cp38-cp38-manylinux1_i686.whl.

File metadata

  • Download URL: logwrap-8.0.0-cp38-cp38-manylinux1_i686.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1

File hashes

Hashes for logwrap-8.0.0-cp38-cp38-manylinux1_i686.whl
Algorithm Hash digest
SHA256 7e9ad9622768b030d74e59bcb519eadf0026fdcdc4fa00dc3596b685d9f5eb63
MD5 4ee7691387e0a0eade9905b90c1992e7
BLAKE2b-256 b929680aaa3940e1f61f58a822df33d3be04b254dae4ace4c4bf8d49af4e8d12

See more details on using hashes here.

File details

Details for the file logwrap-8.0.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: logwrap-8.0.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 182.0 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for logwrap-8.0.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 5ca1d969eb1110e646c0bece3bedfb92f36b5d41eb491686ab706cc66480bf7a
MD5 43a5d2ef3fdd5ade5ba504d5112750b4
BLAKE2b-256 f8750a8f2afea41dd7885dbcba47f211c76c559dc6d22d26abfcc4eb1c383888

See more details on using hashes here.

File details

Details for the file logwrap-8.0.0-cp37-cp37m-win32.whl.

File metadata

  • Download URL: logwrap-8.0.0-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 158.6 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for logwrap-8.0.0-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 9e11da19aeadff3bd2b6382ef63893973c88909802fa7a025e3470d157930771
MD5 c1e4ae0bf4fd20f56bd5e8dba0e62f73
BLAKE2b-256 cdd3afc214e6baa5aaf270d03c4de09a7a610143abcbbb42197e7ac031d7708c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: logwrap-8.0.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1

File hashes

Hashes for logwrap-8.0.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 649c883cd130001e65a05cd644362dd9b01875f96240070ab04c50ab75835d23
MD5 990c12287f1b2c96e24b16be9955684f
BLAKE2b-256 4d83e64be109916221f7f2a659cc1b7263445ff8d7384e525dbd17d3f14d0da0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: logwrap-8.0.0-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 1.0 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1

File hashes

Hashes for logwrap-8.0.0-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 1d592e552993f83470f53f34eaf07bf34db20851055604eb07c084701b98e2b8
MD5 d3740103a0168d0afdac6a094c412a1e
BLAKE2b-256 ca73d7def4ad4f24be0b3f84d4e9181144d658b7baa8df86e3c6476a4788239b

See more details on using hashes here.

File details

Details for the file logwrap-8.0.0-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: logwrap-8.0.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 182.0 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.8

File hashes

Hashes for logwrap-8.0.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 83fd1802a48337c964656d4544483078ffbe9ffc8ad2a3d203fad23b65639720
MD5 36a9a90414ec078b49b0ce8b573a16d9
BLAKE2b-256 0a2fff07abefac64b9172895afc10315c89591dc9de25fc5d73d61667351a656

See more details on using hashes here.

File details

Details for the file logwrap-8.0.0-cp36-cp36m-win32.whl.

File metadata

  • Download URL: logwrap-8.0.0-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 158.7 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.6.8

File hashes

Hashes for logwrap-8.0.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 e8039fb02fcbb83e8f8db678c5c5d14d64f4a73cb3b1e75264f74811d25bcc10
MD5 4e5abfb58dce41ddba72a1fed4fc607f
BLAKE2b-256 bb7bd1a445b741b1e22b4ec149f894113d45ac427a479ea337cde67fed58a00e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: logwrap-8.0.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1

File hashes

Hashes for logwrap-8.0.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 81fdf1be1a28b72bc7b322e76a4da21d716b9f048f51f1caa6559104f023f957
MD5 ce54381f7c0953d88b12bc4c9f0507fa
BLAKE2b-256 25dfb47c5f7860528a3b5c282004f865bcac10cca20fbcc95d90e65ab8a65d31

See more details on using hashes here.

File details

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

File metadata

  • Download URL: logwrap-8.0.0-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 1.0 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1

File hashes

Hashes for logwrap-8.0.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 ef9af8d0853644bccb8dd9d1542b529d7936b26d57b03c9c765339c636668170
MD5 56403fa73d30fab2fd3e7fa665f0e245
BLAKE2b-256 44dc7ee62bdd1d70b8a10aafd1fc1072b26090eafd620515a2d45e60f9e55e83

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