此章节为代码示例,了解即可,不要求掌握:
from functools import wraps,partial
import logging
def attach_wrapper(obj,func=None):
if func is None:
return partial(attach_wrapper,obj)
setattr(obj,func.__name__,func)
return func
def logged(level,name=None,message=None):
def decorate(func):
logname =name if name else func.__module__
log = logging.getLogger(logname)
logmsg = message if message else func.__name__
@wraps(func)
def wrapper(*args,**kwargs):
log.log(level,logmsg)
return func(*args,**kwargs)
@attach_wrapper(wrapper)
def set_level(newlevel):
nonlocal level
level = newlevel
@attach_wrapper(wrapper)
def set_message(newmsg):
nonlocal logmsg
logmsg = newmsg
return wrapper
return decorate
@logged(logging.DEBUG)
def add(x:int,y:int):
return x+y
@logged(logging.CRITICAL,'example')
def spam():
print('Spam!')
logging.basicConfig(level=logging.DEBUG)
print(add(2 ,3))
add.set_message('add called')
print(add(2 ,3))
add.set_level(logging.WARNING)
print(add(2,3))
'''
DEBUG:__main__:add
DEBUG:__main__:add called
WARNING:__main__:add called
5
5
5
'''