By default, the Requests python library writes log messages to the console, along the lines of:
默认情况下,请求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?
我通常对这些消息不感兴趣,并想禁用它们。什么是最好的沉默这些信息或减少请求的冗长?
9 个解决方案
#1
418
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)
#2
52
In case you came here looking for a way to modify logging of any (possibly deeply nested) module, use logging.Logger.manager.loggerDict
to get a dictionary of all of the logger objects, which you can then use as the argument to logging.getLogger
:
如果您正在寻找修改任何(可能是深度嵌套的)模块的日志记录的方法,请使用logger . logger .manager。loggerDict来获取所有logger对象的字典,然后您可以使用它作为logg.getlogger的参数。
import requests
import logging
for key in logging.Logger.manager.loggerDict:
print(key)
# requests.packages.urllib3.connectionpool
# requests.packages.urllib3.util
# requests.packages
# requests.packages.urllib3
# requests.packages.urllib3.util.retry
# PYREADLINE
# requests
# requests.packages.urllib3.poolmanager
logging.getLogger('requests').setLevel(logging.CRITICAL)
# Could also use the dictionary directly:
# logging.Logger.manager.loggerDict['requests'].setLevel(logging.CRITICAL)
#3
23
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用于您的日志消息……只需为您正在使用的库修改它。
#4
12
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')
#5
9
For anybody using logging.config.dictConfig
you can alter the requests library log level in the dictionary like this:
对于任何使用logging.config。可以这样修改字典中的请求库日志级别:
'loggers': {
'': {
'handlers': ['file'],
'level': level,
'propagate': False
},
'requests.packages.urllib3': {
'handlers': ['file'],
'level': logging.WARNING
}
}
#6
0
Kbrose's guidance on finding which logger was generating log messages was immensely useful. For my Django project, I had to sort through 120 different loggers until I found that it was the elasticsearch
Python library that was causing issues for me. As per the guidance in most of the questions, I disabled it by adding this to my loggers:
Kbrose关于查找哪个日志记录器正在生成日志消息的指导非常有用。对于我的Django项目,我必须对120个不同的日志记录程序进行排序,直到我发现对我来说造成问题的是elasticsearch Python库。根据大多数问题的指导,我将它添加到我的日志记录器中,使其失效:
...
'elasticsearch': {
'handlers': ['console'],
'level': logging.WARNING,
},
...
Posting here in case someone else is seeing the unhelpful log messages come through whenever they run an Elasticsearch query.
在这里发布,以防其他人看到无用的日志消息时,他们在运行一个Elasticsearch查询。
#7
-1
simple: just add requests.packages.urllib3.disable_warnings()
after import requests
simple:只添加请求。packages.urllib3.disable_warnings()在导入请求之后。
#8
-1
I'm not sure if the previous approaches have stopped working, but in any case, here's another way of removing the warnings:
我不确定以前的方法是否已经停止工作,但无论如何,这里有另一种方法来删除警告:
PYTHONWARNINGS="ignore:Unverified HTTPS request" ./do-insecure-request.py
PYTHONWARNINGS =“忽略:未经证实的HTTPS请求”。/ do-insecure-request.py
Basically, adding an environment variable in the context of the script execution.
基本上,在脚本执行的上下文中添加一个环境变量。
From the documentation: https://urllib3.readthedocs.org/en/latest/security.html#disabling-warnings
从文档:https://urllib3.readthedocs.org/en/latest/security.html disabling-warnings
#9
-1
If You have configuration file, You can configure it.
如果您有配置文件,您可以配置它。
Add urllib3 in loggers section:
在伐木者部分添加urllib3:
[loggers]
keys = root, urllib3
Add logger_urllib3 section:
添加logger_urllib3部分:
[logger_urllib3]
level = WARNING
handlers =
qualname = requests.packages.urllib3.connectionpool
#1
418
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)
#2
52
In case you came here looking for a way to modify logging of any (possibly deeply nested) module, use logging.Logger.manager.loggerDict
to get a dictionary of all of the logger objects, which you can then use as the argument to logging.getLogger
:
如果您正在寻找修改任何(可能是深度嵌套的)模块的日志记录的方法,请使用logger . logger .manager。loggerDict来获取所有logger对象的字典,然后您可以使用它作为logg.getlogger的参数。
import requests
import logging
for key in logging.Logger.manager.loggerDict:
print(key)
# requests.packages.urllib3.connectionpool
# requests.packages.urllib3.util
# requests.packages
# requests.packages.urllib3
# requests.packages.urllib3.util.retry
# PYREADLINE
# requests
# requests.packages.urllib3.poolmanager
logging.getLogger('requests').setLevel(logging.CRITICAL)
# Could also use the dictionary directly:
# logging.Logger.manager.loggerDict['requests'].setLevel(logging.CRITICAL)
#3
23
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用于您的日志消息……只需为您正在使用的库修改它。
#4
12
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')
#5
9
For anybody using logging.config.dictConfig
you can alter the requests library log level in the dictionary like this:
对于任何使用logging.config。可以这样修改字典中的请求库日志级别:
'loggers': {
'': {
'handlers': ['file'],
'level': level,
'propagate': False
},
'requests.packages.urllib3': {
'handlers': ['file'],
'level': logging.WARNING
}
}
#6
0
Kbrose's guidance on finding which logger was generating log messages was immensely useful. For my Django project, I had to sort through 120 different loggers until I found that it was the elasticsearch
Python library that was causing issues for me. As per the guidance in most of the questions, I disabled it by adding this to my loggers:
Kbrose关于查找哪个日志记录器正在生成日志消息的指导非常有用。对于我的Django项目,我必须对120个不同的日志记录程序进行排序,直到我发现对我来说造成问题的是elasticsearch Python库。根据大多数问题的指导,我将它添加到我的日志记录器中,使其失效:
...
'elasticsearch': {
'handlers': ['console'],
'level': logging.WARNING,
},
...
Posting here in case someone else is seeing the unhelpful log messages come through whenever they run an Elasticsearch query.
在这里发布,以防其他人看到无用的日志消息时,他们在运行一个Elasticsearch查询。
#7
-1
simple: just add requests.packages.urllib3.disable_warnings()
after import requests
simple:只添加请求。packages.urllib3.disable_warnings()在导入请求之后。
#8
-1
I'm not sure if the previous approaches have stopped working, but in any case, here's another way of removing the warnings:
我不确定以前的方法是否已经停止工作,但无论如何,这里有另一种方法来删除警告:
PYTHONWARNINGS="ignore:Unverified HTTPS request" ./do-insecure-request.py
PYTHONWARNINGS =“忽略:未经证实的HTTPS请求”。/ do-insecure-request.py
Basically, adding an environment variable in the context of the script execution.
基本上,在脚本执行的上下文中添加一个环境变量。
From the documentation: https://urllib3.readthedocs.org/en/latest/security.html#disabling-warnings
从文档:https://urllib3.readthedocs.org/en/latest/security.html disabling-warnings
#9
-1
If You have configuration file, You can configure it.
如果您有配置文件,您可以配置它。
Add urllib3 in loggers section:
在伐木者部分添加urllib3:
[loggers]
keys = root, urllib3
Add logger_urllib3 section:
添加logger_urllib3部分:
[logger_urllib3]
level = WARNING
handlers =
qualname = requests.packages.urllib3.connectionpool