深入探究Python logging模块自定义Filter失效的原因
本文分析一个Python logging模块自定义Filter失效的常见问题。代码中自定义了一个Filter,预期只输出包含“custom”关键字的日志信息,但实际只输出了警告、错误和严重错误级别的日志。我们将分析问题原因并提供正确的使用方法。
问题代码及分析:
以下代码演示了问题所在:
立即学习“Python免费学习笔记(深入)”;
import loggingclass customfilter(logging.Filter): def filter(self, record): message = record.getMessage() return 'custom' in messagecustomfilter = customfilter()logger: logging.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')
登录后复制
本文来自互联网或AI生成,不代表软件指南立场。本站不负任何法律责任。