Reputation: 5655
My original intention was just to log the data which I was calling via
logger.debug(' testing ')
calls within my own code. This works.
However, after initializing a logger object at the top of the top of the .py file, it seems that the loggers of one of the libraries which I am using is also calling their logger (requests http lib).
08/24 10:01:34 - requests.packages.urllib3.connectionpool - INFO - connectionpool.py - 202 - Starting new HTTP connection (1): www.technicianonline.com
08/24 10:01:34 - requests.packages.urllib3.connectionpool - DEBUG - connectionpool.py - 296 - "GET / HTTP/1.1" 200 None
08/24 10:01:34 - requests.packages.urllib3.connectionpool - DEBUG - connectionpool.py - 296 - "GET / HTTP/1.1" 200 None
08/24 10:01:34 - root - DEBUG - finder.py - 47 - testing
I only want the root - DEBUG log because that's my own. However all of these requests logs are also being included. If I had to guess it's because python is a dynamic language and the variables are visible, but that does not make that much sense because requests probably initializes it's own logger.
Any ideas? Thanks.
Upvotes: 3
Views: 682
Reputation: 5598
The accepted answer isn't really a practical solution. You shouldn't have to explicitly babysit every logger instantiated by every library you use. Using logging.Filter
isn't really the best idea either, because the standard filter will suppress all unrecognized messages, but you probably still want higher level messages from your libraries.
The correct answer is to follow both suggestions in Greg's comment. Set the root logger higher and use a different logger for your own messages which you can then manage:
In [1]: import logging
In [2]: logging.basicConfig(level=logging.WARNING)
In [3]: l = logging.getLogger('myapp')
In [4]: l.setLevel(logging.DEBUG)
In [5]: l.debug('hello')
DEBUG:test:hello
In [6]: logging.getLogger('library').debug("you won't see me")
In [7]: logging.getLogger('library').critical("you need to see me")
CRITICAL:library:you need to see me
Upvotes: 4
Reputation: 99335
You could do a
logging.getLogger('requests').setLevel(logging.WARNING)
which will restrict the output from all of requests
to WARNING
or above (you can set the level higher, of course, if you'd prefer).
The same approach applies for controlling the verbosity of the loggers of other libraries you might use.
Upvotes: 3
Reputation: 526573
You can add a logging.Filter
which ignores messages from the child loggers.
Upvotes: 1