1

I can set the log level for everything coming from the requests package in the usual fashion:

logging.getLogger('requests').setLevel(...)

But I have a requests.Session which is especially chatty, and I want to set the log level separately for requests originating from that session. The following code sample shows what I'd like to do (the line marked XX).

import requests
import logging

requests.get('https://example.com/').close()
# log output:
# 2020-04-13 19:01:44 [DEBUG] Starting new HTTPS connection (1): example.com:443
# 2020-04-13 19:01:44 [DEBUG] https://example.com:443 "GET / HTTP/1.1" 200 648


s = requests.Session()
s.logger.setLevel(logging.INFO) # XX this doesn't exist, but illustrates what I want to do
s.get('https://example.com/').close() # nothing should be logged
PAG
  • 1,836
  • 1
  • 18
  • 19
  • Does this answer your question? [How do I disable log messages from the Requests library?](https://stackoverflow.com/questions/11029717/how-do-i-disable-log-messages-from-the-requests-library) – mkrieger1 Apr 13 '20 at 22:14
  • See [this answer](https://stackoverflow.com/a/36208664). Maybe getting a list of all existing loggers helps. – mkrieger1 Apr 13 '20 at 22:15
  • @mkrieger1 it clearly does not answer my question: in the first line of my post I make it clear that I know how to disable logging for requests. I just want to disable it for a particular session. – PAG Apr 13 '20 at 22:18
  • I don't think it's possible to do that with sessions. Maybe if you add a [filter](https://docs.python.org/3/library/logging.html#filter-objects) you avoid logging the requests you don't want? – luis.parravicini Apr 13 '20 at 22:45

1 Answers1

0

You can set a decorator for requests and urllib3 (used by requests) in order to optionally provide you a desired logging level just for specific functions.

This would look like this:

import logging
import requests
import functools

logging.basicConfig()

requests_logger = logging.getLogger("requests")
requests_logger_level = requests_logger.getEffectiveLevel()
urllib3_logger = logging.getLogger("urllib3")
urllib3_logger_level = requests_logger.getEffectiveLevel()

url = 'https://pypi.org/pypi/requests/json'


def requests_debug(f):
    @functools.wraps(f)
    def func(*args, **kwargs):
        try:
            requests_logger.setLevel(logging.DEBUG)
            urllib3_logger.setLevel(logging.DEBUG)
            return f(*args, **kwargs)
        finally:
            requests_logger.setLevel(requests_logger_level)
            urllib3_logger.setLevel(urllib3_logger_level)
    return func


@requests_debug
def get_data():
    r = requests.get(url)
    return r.json()['info']['author']


if __name__ == '__main__':
    a = get_data()
    print(a)
Sazzy
  • 1,924
  • 3
  • 19
  • 27
  • this approach is problematic because it's not threadsafe; other threads using requests at the same time that a call to get_data is happening may or may not show their log messages. – PAG May 16 '20 at 17:37