如何禁用请求库中的日志消息?

本文翻译自:How do I disable log messages from the Requests library?

By default, the Requests python library writes log messages to the console, along the lines of: 默认情况下, Requests python库按照以下方式将日志消息写入控制台:

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

I'm usually not interested in these messages, and would like to disable them. 我通常对这些消息不感兴趣,因此想禁用它们。 What would be the best way to silence those messages or decrease Requests' verbosity? 使这些消息静音或降低请求的冗长程度的最佳方法是什么?


#1楼

参考:https://stackoom.com/question/kHKf/如何禁用请求库中的日志消息


#2楼

I found out how to configure requests 's logging level, it's done via the standard logging module. 我发现了如何配置请求的日志记录级别,这是通过标准的日志记录模块完成的。 I decided to configure it to not log messages unless they are at least warnings: 我决定将其配置为不记录消息,除非它们至少是警告:

import logging

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

If you wish to apply this setting for the urllib3 library (typically used by requests) too, add the following: 如果您也希望将此设置应用于urllib3库(通常由请求使用),请添加以下内容:

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

#3楼

Let me copy/paste the documentation section which it I wrote about week or two ago, after having a problem similar to yours: 在遇到类似于您的问题之后,让我复制/粘贴一周或两周前写的文档部分:

import requests
import logging

# these two lines enable debugging at httplib level (requests->urllib3->httplib)
# you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# the only thing missing will be the response.body which is not logged.
import httplib
httplib.HTTPConnection.debuglevel = 1

logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

requests.get('http://httpbin.org/headers')

#4楼

import logging
urllib3_logger = logging.getLogger('urllib3')
urllib3_logger.setLevel(logging.CRITICAL)

In this way all the messages of level=INFO from urllib3 won't be present in the logfile. 这样,来自urllib3的所有level = INFO消息都不会出现在日志文件中。

So you can continue to use the level=INFO for your log messages...just modify this for the library you are using. 因此,您可以继续将level = INFO用作日志消息...只需针对您正在使用的库修改它。


#5楼

For anybody using logging.config.dictConfig you can alter the requests library log level in the dictionary like this: 对于使用logging.config.dictConfig任何人,您都可以在字典中更改请求库日志级别,如下所示:

'loggers': {
    '': {
        'handlers': ['file'],
        'level': level,
        'propagate': False
    },
    'requests.packages.urllib3': {
        'handlers': ['file'],
        'level': logging.WARNING
    }
}

#6楼

简单:只需在import requests后添加requests.packages.urllib3.disable_warnings()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值