Decorator for logging function arguments and return value by human-readable way
Project description
logwrap
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.7 Python 3.8 Python 3.9
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:
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 = ""
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:
Travis CI: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests. Also it’s publishes coverage on coveralls.
coveralls: is used for coverage display.
GitHub: is used for functional tests.
CD systems
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
Built Distributions
Hashes for logwrap-8.2.0.post0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f47ec448a4a6eff43415a0a119ee6a25f9da26f88d787ed56f254fdb08b84105 |
|
MD5 | c445c61fee5a947c78f7dab230bf54cf |
|
BLAKE2b-256 | 96343556a55c93ab2706f21bb86d80e6f8b5e3ab3bdf8acfec80d2a2333e4514 |
Hashes for logwrap-8.2.0.post0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5026feeb19eaca1098db999b81cfe7a4934f453ffc73900ee1a79c3bdc64583 |
|
MD5 | 3d372df0f6616d20a300b62944c043da |
|
BLAKE2b-256 | 3bb7ec3dc56e9c7b1b26cbc7ce7b403416f9d166e4431378bcda7a8d9af47887 |
Hashes for logwrap-8.2.0.post0-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 584d2d8659ae6f2f580572823831ca15c6a648251b4c4cb0b55d230cb8e53fbb |
|
MD5 | d6737ea4596bdcb784f6ea8d2c7b60cd |
|
BLAKE2b-256 | 33f86c973de5873b6ee0c660831da75eec8682ba816e8ef91338e3dbf641c047 |
Hashes for logwrap-8.2.0.post0-cp39-cp39-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8f4597679460e855f1a638f76de5bc6a5e03972d5ddf2955d3ae8b982682b21 |
|
MD5 | f24bce40128e9b623b893936371b51b3 |
|
BLAKE2b-256 | 99099aa872dd1966e8668b8edda699652307ab686a465bd9b86c2e868813b9e0 |
Hashes for logwrap-8.2.0.post0-cp39-cp39-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d49acd92e155046e8d6c7db3b7636905a859f36c69e8b4be08998fdf5811f447 |
|
MD5 | ceff6adc64bd7fb2e413c702696c3ac5 |
|
BLAKE2b-256 | 96172c0b001e0b5947924deb19416fde22f41d25338b694eafa7559a7091dbaa |
Hashes for logwrap-8.2.0.post0-cp39-cp39-manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c0b24921c2f37016a534f553a449964d811b3eda6284f1777fcacb22b6f32d1 |
|
MD5 | 0164c4e7b1db457a42a80c5e75d7eb51 |
|
BLAKE2b-256 | ac69f4bdd50c4c856f3d1b27f09ccce43b28141b39bb76119500f9cea8c3e603 |
Hashes for logwrap-8.2.0.post0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 022daa12754b8896ad129066a309f669aeb3db1950e1aa2b0b049985082655ae |
|
MD5 | a032ba737328d750ea7be6891aa3c32f |
|
BLAKE2b-256 | fac0e209ad5e36563e72d7a5e08a9cf318153402b00eac9740dd470a15bc82e2 |
Hashes for logwrap-8.2.0.post0-cp39-cp39-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b69969b690a748c874a54e850bceabb621abf7b5060570cd891806d73917719c |
|
MD5 | 6c7e2f686195dcdd49886d4e57efa6c0 |
|
BLAKE2b-256 | 9faf5303f864669b77f0433aaee16daa0bcadc56e547652a7e9c4b5a535ae098 |
Hashes for logwrap-8.2.0.post0-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 089e26df43df0ee33c74b4f64fe0f2459dcb26fcd18638a5f60df14af725e996 |
|
MD5 | d20faca7f121ab06807bbf7c5757b49f |
|
BLAKE2b-256 | 661e454db14ba77ae09c36f7295734e0e793c471f25cd1e46da2c00d5a33f489 |
Hashes for logwrap-8.2.0.post0-cp39-cp39-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9bc11cb408d8476c0d38253960dbb01d47c4b49cbe951ead875198e755aed12 |
|
MD5 | 34d1611f9a1626be302827f3d9fde930 |
|
BLAKE2b-256 | 8fe2197babd2a8368324e5f96b9665c4a8af7c49ebcf0c2c7455b7f777fa55fe |
Hashes for logwrap-8.2.0.post0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c120cc11a843c55f40ca955f106a58e2717f86cb0047a0cafc35bd5add5ad3a |
|
MD5 | a54b370f463882bec34b2b2e3fb846ea |
|
BLAKE2b-256 | c4d8d11480232a82fe93e1f81c8dc698c810942929bf5b5f19f0fc839d89eb37 |
Hashes for logwrap-8.2.0.post0-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 396c9f5b6cf9fb3ea43d487757a308974723149637f87a9a4212feffabecb736 |
|
MD5 | 8b83915929979d227e45f361f9841e64 |
|
BLAKE2b-256 | 2eaadb7a41f7a13c17eae300a73bbdb88f55645480cf777c82e7d6246f918e9c |
Hashes for logwrap-8.2.0.post0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ed69631d70e2c074af0e1611725230188ff6c35ddbfb5b1b35ef398f2c0ea95 |
|
MD5 | 014e8408e447e881aae63b6f43a4f852 |
|
BLAKE2b-256 | 7530930028627acef756519c4d8b868e774874643d68a87bf734ab94aaa04745 |
Hashes for logwrap-8.2.0.post0-cp38-cp38-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50e9860ee24c9ab8c423e590749ca35c42786db5f35c13fe80bd9e6d9adac973 |
|
MD5 | 6185c406027950213f3caf9b7f34b3d5 |
|
BLAKE2b-256 | f8af994a062a1869cc2404c51132d5dadcc9573e447bfc690488347a824e141a |
Hashes for logwrap-8.2.0.post0-cp38-cp38-manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc6b6063311fd751443e0d6b02132dc2835fcf15e33360fd401a0a5f3b4a325e |
|
MD5 | 5ae6c77b34ed5eb208caac798da12e7a |
|
BLAKE2b-256 | f7c5c4ec17835293761570bed7411377d0a01831a8d3d1cc211feccad9e15aed |
Hashes for logwrap-8.2.0.post0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9eb84d8362c83daaca63ed95bbc3272672ee36056a3b7646bbca39a4d6be16c1 |
|
MD5 | 3d7a4325b8b90ad54be8a4c092a92d83 |
|
BLAKE2b-256 | e661e33ada9f443cfb0432a0b23f822a05818732c8e74b6125c286076c34c7ef |
Hashes for logwrap-8.2.0.post0-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77c27a597be56f0f6052fd4326a835142d8147f35b56f4a15ca0185c629513ed |
|
MD5 | 6f570587185745575113605204df6ff2 |
|
BLAKE2b-256 | f93bcf7860b0df84db91f8fbbcba52d6c32376b1e59922195fdc81ff26f9802c |
Hashes for logwrap-8.2.0.post0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2be449cc502f2ae6627bc47b9226e52b7b83efdaa904612cec494244c16f42e |
|
MD5 | f0085a4cf3c64854110725b8b5071f7a |
|
BLAKE2b-256 | 7b758444b81004e86b401ad6836c3bd6e968031f8a84b27e159ec470d6cd36dc |
Hashes for logwrap-8.2.0.post0-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52fd5786c59966dc5103f1724077b40d4dd82e4b55883657bd5d4577275fdd2c |
|
MD5 | e813c4bc7278bc6f49b5403d77e5abaa |
|
BLAKE2b-256 | 739d8083c0113125c0c9323ae4a9796c33a09d9ab262258b11d75ff70be61063 |
Hashes for logwrap-8.2.0.post0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30ba6e0bd43b21092ca1f6e83bba386427e91f9834f7f6f88d4e0f44d7f05d01 |
|
MD5 | b47788b986cc28e07b707cb0f8a25bdc |
|
BLAKE2b-256 | 364ae9a47ddf631ee684fc6f5ec93aa9706f02631ecb94f4f2e84cdf487b4837 |
Hashes for logwrap-8.2.0.post0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 039fac85cde1e6e57373811ff19e8154d60cc43f88a35d2ac89a6679efe8c1b7 |
|
MD5 | 42a8caa27b1604888796a1276eacb023 |
|
BLAKE2b-256 | f4858e95a6d30b17175713557b9fbfbac9b6bd093c46a75c272863e01911e95e |
Hashes for logwrap-8.2.0.post0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb9c0b21b3e0425cfdd5b61fb3939caaa090725e0af218fc85f397ea45b975a7 |
|
MD5 | 5817e5ebbfad2eefd3e7cb636f2dacc3 |
|
BLAKE2b-256 | 550ee0124ddf3ef54c929d7df4618135d9b1cdfa38c50d2af8dd92c8e374c1a0 |
Hashes for logwrap-8.2.0.post0-cp37-cp37m-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04ea1ea0519b2c302cf0e934091c86d0088b246aecce73915bcb1ce670b41c16 |
|
MD5 | f21a03ac7907b0dfddf850affcec92ee |
|
BLAKE2b-256 | a635ab4c5f640e7f34f7c401d8664f4c7f9359724520377fc96ad874fd7540a7 |
Hashes for logwrap-8.2.0.post0-cp37-cp37m-manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 928ee1daa2861c9573282022eed4a2fb01909047cc57357b91a67c3c4f2fcb52 |
|
MD5 | 6f92352bca744d79cc73fcc91f98e9c9 |
|
BLAKE2b-256 | 383ad4a16208b0ec807e464347becf503583ae4636f78560946d31f65c343120 |
Hashes for logwrap-8.2.0.post0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c97bfabdf0daaec150dbc15876599bae3535a8c31adcfd2812b19aaff443ccc1 |
|
MD5 | 2fa3fac4d7a1c8a61a01afc57f9f52be |
|
BLAKE2b-256 | dfef63208c831263c4418d145b38aa3b38f447e866e62f77b524615368507c3a |
Hashes for logwrap-8.2.0.post0-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d62def031a3b67ef3ecc8cac88b3d459a2a31141c4dd16cba8f5b00bfe77548 |
|
MD5 | 1b43b83d0827c24e8a6cda40bf2cc6b1 |
|
BLAKE2b-256 | abaff7f7c5ed266818061cd26c5fe6ed85cbfcdfd57e17cf28462c30140c724c |
Hashes for logwrap-8.2.0.post0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 203d90493457aa3b033ef392fc53d71760007a1be502faaa75886e32fba8a83a |
|
MD5 | 57ef6c1bfbb365eb1def3249d2510d95 |
|
BLAKE2b-256 | 72042a187b6646f7e04f880fce739704b2adc52f0db041895eacbb956317b2d0 |
Hashes for logwrap-8.2.0.post0-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01fcc3953f40e3d902fcce2c4ce775f587d9af85c247b0c397c5a7134629b01d |
|
MD5 | b3f882abe2848349bb76c35f4a52114c |
|
BLAKE2b-256 | f1e1b7ac5643b9cfc2835d74e27e6454b3946548e8466125a692e55428b403e7 |