Decorator for logging function arguments 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.
Pros:
Free software: Apache license
Open Source: https://github.com/penguinolog/logwrap
PyPI packaged: https://pypi.python.org/pypi/logwrap
Self-documented code: docstrings with types in comments
Tested: see bages on top
Support miltiple Python versions:
Python 2.7 Python 3.4 Python 3.5 Python 3.6 PyPy
This package also includes helpers:
pretty_repr
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
)
Usage examples:
@logwrap.logwrap()
def foo():
pass
is equal to:
@logwrap.logwrap
def foo():
pass
Get decorator for use without parameters:
get_logs = logwap.logwrap() # set required parameters via arguments
@get_logs
def foo():
pass
Limitations: * return value from awaitable objects (async def(…) is not accessible - on call asyncio object is returned.
nested wrapping (@logwrap @deco2 …) is not parsed under python 2.7: funcsigs limitation. Please set logwrap as the first level decorator.
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
py2_str=False, # use bytes for python 2 __repr__ and __str__
)
Limitation: Dict like objects is always marked inside {} for readability, even if it is collections.OrderedDict (standard repr as list of tuples).
PrettyFormat
PrettyFormat is the main formatting implementation class. on pretty_repr instance of this class is created and executed. Object signature:
def __init__(
self,
formatters=None, # Currently only legacy pretty_repr formatters is supported, will be extended in the future
keyword='repr', # Currently 'repr' is supported, will be extended in the future
max_indent=20, # maximum allowed indent level
indent_step=4, # step between indents
py2_str=False, # use bytes for python 2 __repr__ and __str__
)
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.
Testing
The main test mechanism for the package logwrap is using tox. Test environments available:
pep8 py27 py34 py35 pypy pylint docs
Also possible to run python setup.py test for unit tests and python setup.py flake8 for code style tests, but it requires all package dependencies to be installed.
CI systems
For code checking several CI systems is used in parallel:
Travis CI: is used for checking: PEP8, pylint, installation possibility and unit tests. Also it’s publishes coverage on Codacy.
Codacy: is used for statical analysis and 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
Built Distribution
Hashes for logwrap-1.0.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3b91ca06b253eca16f6b1a8f0a25fe2394916506a936d16562bf1c3a60b8286 |
|
MD5 | 56ce1f9030a1d2ddaa46c3a91e65137f |
|
BLAKE2b-256 | 96d264883f56b6d845ab7f8e5a7fa819251fed5ffe453a3755cd39f41ec1b1d2 |