Skip to content

Instantly share code, notes, and snippets.

@dz0
Last active September 30, 2020 06:50
Show Gist options
  • Select an option

  • Save dz0/0bd82db0e5d19b79009f63a6d9ba46de to your computer and use it in GitHub Desktop.

Select an option

Save dz0/0bd82db0e5d19b79009f63a6d9ba46de to your computer and use it in GitHub Desktop.
function_call_log_decorator.py
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