这是我使用的代码:
import logging class decorator_class(object): def __init__(self, original_function): self.original_function = original_function self.logger = logging.getLogger(original_function.__name__) self.logger.setLevel(logging.INFO) self.file_handler = ##logging.FileHandler('{}.log'.format(self.original_function.__name__)) #can I use different log file whereever the class self.logger.addHandler(file_handler) def __call__(self, *args, **kwargs): logger.info('function {} will be executed '.format(self.original_function.__name__)) original_function(*args, **kwargs) @decorator_class def display_info(name, age): logger.info('display_info ran with arguments ({}, {})'.format(name, age)) display_info('Tim', 25) @decorator_class def add_num(x, y): logger.info('add_num ran with arguments ({}, {})'.format(x, y)) z=x+y return z add_num(2,3)
这是错误:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) in () 17 18 ---> 19 @decorator_class 20 def display_info(name, age): 21 logger.info('display_info ran with arguments ({}, {})'.format(name, age)) in __init__(self, original_function) 8 self.logger.setLevel(logging.INFO) 9 self.file_handler = logging.FileHandler('{}.log'.format(self.original_function.__name__)) ---> 10 self.logger.addHandler(file_handler) 11 12 NameError: name 'file_handler' is not defined