My logging setting look like
import requests
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('BBProposalGenerator')
When I run this, I get logs as
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
INFO:BBProposalGenerator:created proposal: 763099d5-3c8a-47bc-8be8-b71a593c36ac
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
INFO:BBProposalGenerator:added offer with cubeValueId: f23f801f-7066-49a2-9f1b-1f8c64576a03
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
INFO:BBProposalGenerator:evaluated proposal: 763099d5-3c8a-47bc-8be8-b71a593c36ac
How can I suppress the log statements from requests
package? so that I only see logs from my code
INFO:BBProposalGenerator:created proposal: 763099d5-3c8a-47bc-8be8-b71a593c36ac
INFO:BBProposalGenerator:added offer with cubeValueId: f23f801f-7066-49a2-9f1b-1f8c64576a03
INFO:BBProposalGenerator:evaluated proposal: 763099d5-3c8a-47bc-8be8-b71a593c36ac
Thanks
what you want to do is apply a filter to all of the loggers so that you can control what is emitted. The only way I could find to apply of filter to all of the loggers is by initializing the logging Logger class with something derived from logging.Logger and applying the filter there. As so:
And then all you have to do is, the before any loggers are instantiated set the default logger class as your derived class, as so:
Hope this helps!
You called
basicConfig()
with a level oflogging.INFO
, which sets the effective level toINFO
for the root logger, and all descendant loggers which don't have explicitly set levels. This includes therequests
loggers, and explains why you're getting these results.Instead, you can do
which will leave the level at its default value of
WARNING
, but add a handler which outputs log messages to the console. Then, set the level on your logger toINFO
:Now,
INFO
and higher severity events logged to loggerBBProposalGenerator
or any of its descendants will be printed, but the root logger (and other descendants of it, such asrequests.XXX
) will remain atWARNING
level and only showWARNING
or higher messages.Of course, you can specify a higher level in the
basicConfig()
call - if you specifiedERROR
, for example, you would only seeERROR
or higher from all the other loggers, butINFO
or higher fromBBProposalGenerator
and its descendants.