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

5

我正在使用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)

所以你想从其他东西获取“INFO”消息,但包括serf - jonrsharpe
是的,实际上等待答案的几分钟后我自己解决了。 - sdot257
1个回答

5
记录器名称是serf-rpc-client,所以这应该可以工作。
serf_logger = logging.getLogger('serf-rpc-client')
serf_logger.setLevel(logging.WARNING)

是的,我看源代码后弄明白了 - 哎呀!有没有更快的方法知道这个? - sdot257
2
阅读日志 - 01-04 14:57 serf-rpc-client INFO - 第一个是日期,然后是记录器名称,最后是级别。如果这个问题有帮助,请让我知道。 - masnun
啊,好的,我注意到了。非常感谢。 - sdot257
我会的,它不让我。我得等一下。 :-) - sdot257

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接