Python: how to suppress logging statements from third party libraries? [duplicate]
Asked Answered
H

2

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
Huckaback answered 21/2, 2014 at 20:24 Comment(3)
logging.getLogger('requests').setLevel(logging.ERROR)?Janelljanella
I got the answer from #11030217Huckaback
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 solved that issue for me.Fujio
P
20

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.

Pyrophyllite answered 21/2, 2014 at 21:47 Comment(3)
What if I want to share the logger across modules?Fong
@Fong Loggers are singletons, so you don't need to pass them between modules. Just call logging.getLogger(<NAME>) and for a given name you'll always get the same logger object.Pyrophyllite
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.Amalia
E
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!

Eddaeddana answered 21/2, 2014 at 20:59 Comment(1)
This is not necessary - see my answer.Pyrophyllite

© 2022 - 2024 — McMap. All rights reserved.