class CustomFilter(logging.Filter):
def filter(self, record):
message = record.getMessage()
return 'custom' in message
customFilter = CustomFilter()
logger: Logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addFilter(customFilter)
logger.debug('This is a debug message with custom keyword')
logger.info('This is an info message with custom keyword')
logger.warning('This is a warning message with custom keyword')
logger.error('This is an error message with custom keyword')
logger.critical('This is a critical message with custom keyword')
为什么上述代码不会在控制台打印出 debug 和 info 级别的日志信息?
只会输出:
This is a warning message with custom keyword
This is an error message with custom keyword
This is a critical message with custom keyword
不是filter的问题,是你使用的问题
原因
正确的使用方法是