我需要两个处理程序。一个用于文件记录,另一个用于控制台中的流记录。我需要为每个处理程序指定级别。请注意,我的级别将类似于以下级别。
流处理程序 --> 信息
文件处理程序 --> 警告、错误、严重
这是我的代码。
# Create a custom logger
logger = logging.getLogger('DBMQ')
logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
logger.addHandler(stream_handler)
file_handler = logging.FileHandler('./data/file.log')
file_handler.setLevel(logging.WARNING)
logger.addHandler(file_handler)
现在,它必须像这样运行:
logger.debug('hey') # Nothing should happen
logger.info('hey') # only stream handler should log this message
logger.warning('hey') # only file handler should log this message but the stream does too
logger.error('hey') # only file handler should log this message but the stream does too
logger.critical('hey') # only file handler should log this message but the stream does too
我需要禁止流处理程序记录warning
、error
和critical
记录级别。有没有办法过滤这个处理程序?