Last active
September 30, 2020 06:50
-
-
Save dz0/0bd82db0e5d19b79009f63a6d9ba46de to your computer and use it in GitHub Desktop.
function_call_log_decorator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def log_call(func): | |
| """generic decorator""" | |
| import functools | |
| import inspect | |
| @functools.wraps(func) | |
| def wrapper(*args, **kwargs): | |
| try: | |
| args_repr = list(map(repr, args)) + [f"{key}={repr(val)}" for key, val in kwargs.items()] | |
| if inspect.ismethod(func) or '.' in func.__qualname__: | |
| args_repr[0] = 'self' | |
| args_repr = ', '.join(args_repr) | |
| log.debug(f"call: {func.__qualname__}({args_repr})") | |
| return func(*args, **kwargs) | |
| except Exception as e: | |
| error_msg = 'And error has occurred at /' + func.__name__ + '\n' | |
| log.error(str(e)) | |
| # log.exception(error_msg) | |
| raise | |
| return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment