Skip to content

Instantly share code, notes, and snippets.

@achavez
Last active May 3, 2016 17:26
Show Gist options
  • Save achavez/981acda7badaae8bb1cddba1217c43c8 to your computer and use it in GitHub Desktop.
Save achavez/981acda7badaae8bb1cddba1217c43c8 to your computer and use it in GitHub Desktop.
Python class method call logger
import inspect
import datetime
class ClassToLog(object):
pass
def logging_decorator(fn, fn_name):
def logging_method(*args, **kwargs):
frame = inspect.currentframe()
print('[%s] %s > %s' % (datetime.now(), frame.f_back.f_code.co_name,
fn_name,))
return fn(*args, **kwargs)
return logging_method
for name, fn in inspect.getmembers(ClassToLog):
if isinstance(fn, types.UnboundMethodType):
setattr(ClassToLog, name, logging_decorator(fn, name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment