不幸的是
logging使用功能代码对象来推断名称。您可以通过使用
extra关键字参数为记录指定一些其他属性来解决此问题,然后可以在格式化期间使用这些属性。您可以执行以下 *** 作:
logging.basicConfig( level=logging.DEBUG, format='%(real_func_name)20s - %(message)s',)...logging.info(statement, extra={'real_func_name': func.__name__})
这种方法的唯一缺点是您
extra每次都必须传入字典。为了避免这种情况,您可以使用自定义格式器并覆盖它
funcName:
import loggingfrom functools import wrapsclass CustomFormatter(logging.Formatter): """Custom formatter, overrides funcName with value of name_override if it exists""" def format(self, record): if hasattr(record, 'name_override'): record.funcName = record.name_override return super(CustomFormatter, self).format(record)# setup logger and handlerlogger = logging.getLogger(__file__)handler = logging.StreamHandler()logger.setLevel(logging.DEBUG)handler.setLevel(logging.DEBUG)handler.setFormatter(CustomFormatter('%(funcName)20s - %(message)s'))logger.addHandler(handler)def log_and_call(statement): def decorator(func): @wraps(func) def wrapper(*args, **kwargs): # set name_override to func.__name__ logger.info(statement, extra={'name_override': func.__name__}) return func(*args, **kwargs) return wrapper return decorator@log_and_call("This should be logged by 'decorated_function'")def decorated_function(): logger.info('I ran')decorated_function()
您要做什么?
% python logging_test.py decorated_function - This should be logged by 'decorated_function' decorated_function - I ran
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)