问题
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
回答1:
You called basicConfig()
with a level of logging.INFO
, which sets the effective level to INFO
for the root logger, and all descendant loggers which don't have explicitly set levels. This includes the requests
loggers, and explains why you're getting these results.
Instead, you can do
logging.basicConfig()
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 to INFO
:
logger = logging.getLogger('BBProposalGenerator')
logger.setLevel(logging.INFO)
Now, INFO
and higher severity events logged to logger BBProposalGenerator
or any of its descendants will be printed, but the root logger (and other descendants of it, such as requests.XXX
) will remain at WARNING
level and only show WARNING
or higher messages.
Of course, you can specify a higher level in the basicConfig()
call - if you specified ERROR
, for example, you would only see ERROR
or higher from all the other loggers, but INFO
or higher from BBProposalGenerator
and its descendants.
回答2:
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:
class MyFilter(logging.Filter):
def filter(self, record):
if record.name != 'BBProposalGenerator':
return False
return True
class MyLogger(logging.Logger):
def __init__(self, name):
logging.Logger.__init__(self, name)
self.addFilter(MyFilter())
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:
logging.setLoggerClass(MyLogger)
logging.basicConfig(level=logging.INFO)
Hope this helps!
来源:https://stackoverflow.com/questions/21944445/python-how-to-suppress-logging-statements-from-third-party-libraries