By default, the Requests python library writes log messages to the console, along the lines of:
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?
I'm not sure if the previous approaches have stopped working, but in any case, here's another way of removing the warnings:
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
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:
If you wish to apply this setting for the urllib3 library (typically used by requests) too, add the following:
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. The returned names can then be used as the argument tologging.getLogger
:Per user136036 in a comment, be aware that this method only shows you the loggers that exist at the time you run the above snippet. If, for example, a module creates a new logger when you instantiate a class, then you must put this snippet after creating the class in order to print its name.
Setting the logger name as
requests
orrequests.urllib3
did not work for me. I had to specify the exact logger name to change the logging level.First See which loggers you have defined, to see which ones you want to remove
And you will see something like this:
{...'urllib3.poolmanager': <logging.Logger object at 0x1070a6e10>, 'django.request': <logging.Logger object at 0x106d61290>, 'django.template': <logging.Logger object at 0x10630dcd0>, 'django.server': <logging.Logger object at 0x106dd6a50>, 'urllib3.connection': <logging.Logger object at 0x10710a350>,'urllib3.connectionpool': <logging.Logger object at 0x106e09690> ...}
Then configure the level for the exact logger:
simple: just add
requests.packages.urllib3.disable_warnings()
afterimport requests
In this way all the messages of level=INFO from urllib3 won't be present in the logfile.
So you can continue to use the level=INFO for your log messages...just modify this for the library you are using.