19

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
vvvvv
  • 25,404
  • 19
  • 49
  • 81
daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • 1
    `logging.getLogger('requests').setLevel(logging.ERROR)`? – jfs Feb 21 '14 at 20:29
  • 2
    I got the answer from https://stackoverflow.com/questions/11029717/how-do-i-disable-log-messages-from-the-requests-library – daydreamer Feb 21 '14 at 21:12
  • This is definitely not the same as the listed question. That question pertains to the `requests` module only. The title, which brought me here, refers to *all* 3rd-party libraries. [@VinaySajip's answer below](https://stackoverflow.com/a/21945847/1107226) solved that issue for me. – leanne Mar 03 '23 at 23:52

2 Answers2

19

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
  • 95,872
  • 14
  • 179
  • 191
  • 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
  • Probably the only answer I've searched that actually explains the reasoning on what's happening under the hood rather than just replying with _how_ to achieve what's being asked. – shriek Jul 23 '23 at 22:14
6

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
  • 699
  • 1
  • 7
  • 16
Paul Mikesell
  • 1,011
  • 8
  • 6