原文来源: https://stackoverflow.com/questions/11029717/how-do-i-disable-log-messages-from-the-requests-library

问:
默认情况下,python的requests的库里面会有下面的日志:

Starting new HTTP connection (1): example.com  http://example.com:80 "GET / HTTP/1.1" 200 606

我对这类日志并不关心,我想怎么样才能够把他们屏蔽掉。禁止或者减少这一类log的最佳实践是什么呢?

答:
我发现能够通过配置request的logging level,这是通过标准库中的logging模块做到的。
我设定一般情况下不显示log信息,除非request里面是有warnings警报等级的日志。
具体代码实现:

import logging
logging.getLogger("requests").setLevel(logging.WARNING)

其他库也可以同样进行设置,例如你想要设定urllib3的库的log

import logging
logging.getLogger("urllib3").setLevel(logging.WARNING)

相关文章:

  • 2021-08-19
  • 2022-12-23
  • 2021-12-17
  • 2021-05-19
  • 2022-12-23
  • 2021-09-10
  • 2021-07-27
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
相关资源
相似解决方案