from functools import wraps
import inspect
import logging
logger = logging.getLogger('func_log')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('test.log')
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(asctime)s][%(thread)d][%(filename)s][line: %(lineno)d][%(levelname)s] ## %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
def decorator(function):
@wraps(function)
def inner(*args, **kwargs):
result = function()
logger.debug('%s is excuted!'%function.__name__)
return result
return inner
@decorator
def func1():
print('%s is excuted'% inspect.stack()[0][3])
@decorator
def func2():
pass
if __name__ == '__main__':
func1()
func2()