17

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

Vinay Sajip
  • 90,050
  • 14
  • 168
  • 180
daydreamer
  • 80,741
  • 175
  • 429
  • 691

2 Answers2

14

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.

Vinay Sajip
  • 90,050
  • 14
  • 168
  • 180
  • What if I want to share the logger across modules? – Tengerye Oct 16 '21 at 07:49
  • 1
    @Tengerye Loggers are singletons, so you don't need to pass them between modules. Just call `logging.getLogger()` and for a given name you'll always get the same logger object. – Vinay Sajip Oct 16 '21 at 18:34
5

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!

xilopaint
  • 601
  • 6
  • 16
Paul Mikesell
  • 1,001
  • 8
  • 6