Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

logging

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

like image 325
daydreamer Avatar asked Feb 21 '14 20:02

daydreamer


People also ask

What is the best practice for third party libraries which use logging?

A third party library which uses logging should not spew logging output by default which may not be wanted by a developer/user of an application which uses it. The best practice is to not emit library logs by default and let the user of the library determine whether they want to receive and handle logs in the application.

How to define a logger in Python?

You can (and should) define your logger by creating an object of a Logger class, especially if your application has the multiple modules. Let’s have the look at some of the classes and functions in Python module. The most common classes defined in the logging module are the following.

How to disable logging from imported modules in Python?

How to disable logging from imported modules in Python? You can disable logging from imported modules using the logging module. You can configure it to not log messages unless they are at least warnings using the following code: If you dont want to write the module name as a string, you can also use imported_module.__name__.

What is a Python log message?

A log message can store information like the current status of a program or where the program is running. If an error occurs, developers can quickly find the line of code that causes the issue and act upon that. Python provides a quite powerful and flexible built-in logging module with many advanced features.


1 Answers

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.

like image 92
Vinay Sajip Avatar answered Oct 05 '22 22:10

Vinay Sajip