Last active
May 3, 2016 17:26
-
-
Save achavez/981acda7badaae8bb1cddba1217c43c8 to your computer and use it in GitHub Desktop.
Python class method call logger
This file contains 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
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