如何在 Python 中禁用来自第三方模块的 INFO 日志记录

How to disable INFO logging from a third party module in Python

我正在使用 Python 查询 Serf 集群,但我想抑制来自 Serf 的 INFO 数据。我试图覆盖它,以便它只会打印 WARNING 消息,但它拒绝接受它。

输出:

01-04 14:57 root         INFO     Connecting to cluster
01-04 14:57 serf-rpc-client INFO     will connect to [('myhost.localdomain.local', 7373, {})]
01-04 14:57 serf-rpc-client INFO     trying to connect to myhost.localdomain.local:7373
01-04 14:57 serf-rpc-client INFO     connected to myhost.localdomain.local:7373
01-04 14:57 serf-rpc-client INFO     trying to request command: <RequestHandshake: handshake, 0, {'Version': 1}>
01-04 14:57 serf-rpc-client INFO     trying to request command: <RequestAuth: auth, 1, {'AuthKey': 'thundercats'}>
01-04 14:57 serf-rpc-client INFO     trying to request command: <RequestMembers: members, 2, {'Status': 'failed'}>
01-04 14:57 serf-rpc-client INFO     successfully handshaked
01-04 14:57 serf-rpc-client INFO     successfully authed
01-04 14:57 root         INFO     myhost123.localdomain.local has left the cluster

记录代码

logging.basicConfig(level=logging.INFO,
                format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                datefmt='%m-%d %H:%M',
                filename='/var/log/ocd_watcher.log',
                filemode='w')

serf_logger = logging.getLogger('serf')
serf_logger.setLevel(logging.WARNING)

console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(default_formatter)

logger = logging.getLogger()
logger.addHandler(console_handler)
logger.addHandler(serf_logger)

记录器名称是 serf-rpc-client 所以这应该可以工作

serf_logger = logging.getLogger('serf-rpc-client')
serf_logger.setLevel(logging.WARNING)