Skip to main content

logwrap

https://github.com/python-useful-helpers/logwrap/workflows/Python%20package/badge.svg 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

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

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,
    max_iter=0,  # forwarded to the pretty_repr, max number of items in the Iterable before ellipsis. Unlimited if 0.
    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/CD systems

GitHub: is used for functional tests.

Metadata

Release files for logwrap 11.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logwrap 11.2.0
File Size Uploaded
logwrap-11.2.0.tar.gz 43.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logwrap 11.2.0
File Interpreter ABI Platform
logwrap-11.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 72.1 kB

Release files / logwrap-11.2.0.tar.gz

Download URL logwrap-11.2.0.tar.gz
Size 43.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8c6045ad823b5bc2a1311e963afff2cc15ec4af72446280c40ebe1a7074fabbe
BLAKE2b-256 checksum
How to use checksums
8ca77046e03f855bb051ad33f682dd77622176aff9ae7acf9e3121c72e01eb3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 29, 2026.

Transparency log

Release files / logwrap-11.2.0-py3-none-any.whl

Download URL logwrap-11.2.0-py3-none-any.whl
Size 29.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c100972311220219867786692ebeb9880ca336174a90b511eb3221c5460ec760
BLAKE2b-256 checksum
How to use checksums
560950d9d8d0a80894218083cb7b059f7b80be397d0fe4008f14b84a606b9eaf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 29, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

11.2.0 This release

2 release files

11.1.2

2 release files

11.1.1

2 release files

11.0.0

2 release files

9.0.1

30 release files

8.3.0

23 release files

8.2.0

27 release files

8.1.1

29 release files

8.1.0

29 release files

8.0.5

29 release files

8.0.4

29 release files

8.0.2

1 release file

8.0.0

14 release files

7.0.0

14 release files

6.6.0

14 release files

6.5.0

14 release files

6.4.0

14 release files

6.3.9

14 release files

6.3.8

14 release files

6.3.7

12 release files

6.3.6

10 release files

6.3.5

10 release files

6.3.4

10 release files

6.3.3

6 release files

6.3.2

10 release files

6.3.1

10 release files

6.3.0

10 release files

6.2.3

4 release files

6.2.2

4 release files

6.2.1

4 release files

6.1.1

6 release files

6.1.0

6 release files

6.0.1

6 release files

6.0.0

6 release files

5.8.0

5 release files

5.7.0

5 release files

5.6.0

5 release files

5.5.1

1 release file

5.5.0

1 release file

5.4.0

4 release files

5.3.9

8 release files

5.3.8

11 release files

5.3.1

8 release files

5.3.0

8 release files

5.2.2

8 release files

5.2.1

8 release files

5.2.0

8 release files

5.1.0

8 release files

5.0.3

2 release files

5.0.2

8 release files

5.0.1

8 release files

5.0.0

8 release files

4.10.0

4 release files

4.9.5

4 release files

4.9.4

4 release files

4.9.3

4 release files

4.9.2

4 release files

4.9.1

4 release files

4.9.0

4 release files

4.0.5

2 release files

4.0.4

2 release files

4.0.3

2 release files

4.0.2

10 release files

4.0.1

10 release files

3.3.0

7 release files

3.2.2

7 release files

3.2.1

14 release files

3.2.0

7 release files

3.1.0

7 release files

3.0.2

7 release files

3.0.1

7 release files

3.0.0

7 release files

2.7.3

7 release files

2.7.2

1 release file

2.6.0

7 release files

2.5.1

2 release files

2.5.0

2 release files

2.4.2

2 release files

2.4.1

2 release files

2.4.0

2 release files

2.3.5

2 release files

2.3.4

2 release files

2.3.3

2 release files

2.3.2

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.6

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.9.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page