装饰器类
class logit(object):
_logfile = 'out.log'
def __init__(self, func):
self.func = func
def __call__(self, *args):
log_string = self.func.__name__ + " was called"
print(log_string)
# 打开logfile并写入
with open(self._logfile, 'a') as opened_file:
# 现在将日志打到指定的文件
opened_file.write(log_string + '\n')
# 现在,发送一个通知
self.notify()
# return base func
return self.func(*args)
def notify(self):
# logit只打日志,不做别的
pass最后更新于
这有帮助吗?