warnings.filterwarnings(“ignore”) 不会忽略 logging.warning

warnings.filterwarnings("ignore") doesn't ignore logging.warning

提问人:Gerry 提问时间:3/21/2021 更新时间:3/21/2021 访问量:2926

问:

我想知道为什么不能忽略所有警告消息。这里有什么要求吗?warnings.filterwarnings("ignore")

>>> import logging
>>> import warnings
>>> warnings.filterwarnings("ignore")
>>> logging.warn("This should not be printed")
WARNING:root:This should not be printed
>>>
python-3.x 日志记录 警告 suppress-warnings

评论

0赞 milahu 10/16/2023
另请参阅 Python 日志记录 - 禁用导入模块的日志记录

答:

2赞 avayert 3/21/2021 #1

模块和模块有意分开的过滤器。如果要过滤掉模块中的警告消息,则必须使用模块中的相应功能,例如过滤器或适当的调用。warningloggingloggingloggingsetLevel

评论

1赞 Gerry 3/21/2021
谢谢@avayert。你能举一个简单的例子吗?