我设置了一个python记录器,我希望能够将我创建的过滤器添加到日志的logging.ERROR级别。
我试过这个:
class MyFilter(object):
def __init__(self, level):
self.__level = level
def filter(self, logRecord):
return logRecord.levelno <= self.__level
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(levelname)-8s %(msg)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.addFilter(MyFilter(logging.ERROR))
当我尝试这个时,我收到一个错误:TypeError: object of type 'int' has no len()
如何将过滤器添加到特定的日志记录级别?