I import many third party modules that have various levels of logging (ERROR, WARNING, INFO, DEBUG) within them. I would like to change the log level of a given log statement.
For example, I can do something like this:
import logging
import requests
logging.basicConfig()
logging.getLogger().setLevel(logging.NOTSET)
requests.get('http://www.google.com')
This will output something like this:
DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): www.google.com
DEBUG:requests.packages.urllib3.connectionpool:http://www.google.com:80 "GET /
HTTP/1.1" 200 4674
<Response [200]>
You can see that this comes from this source code. You can clearly see it's calling a log.debug
which corresponds to the DEBUG message above. How could I change this to log.info
if I wanted to?
Note that I'm aware of setLevel
, which would suppress messages below a certain level. That is not what I'm looking for.
Suboptimal solution #1 I could extend the requests
module, and overwrite that specific function. I don't want to maintain
Suboptimal solution #2 I could overwrite the constant for DEBUG within that module CRITICAL:
requests.packages.urllib3.logging.DEBUG = requests.packages.urllib3.logging.CRITICAL
requests.get('http://www.google.com')
CRITICAL:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): www.google.com
CRITICAL:requests.packages.urllib3.connectionpool:http://www.google.com:80 "GET / HTTP/1.1" 200 4683
<Response [200]>
What is the best way to accomplish this?