5

How can I completely remove any logging from requests module in Python? I don't need to set even CRITICAL level. Smth like this

import logging
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.CRITICAL)

but without any messages, even CRITICAL.

3
  • 1
    The requests module doesn't log messages. The embedded urllib3 library does, but doesn't log any CRITICAL level messages. Your code more than suffices. Commented Jun 21, 2014 at 17:27
  • Possible duplicate of How do I disable log messages from the Requests library? Commented Apr 28, 2016 at 8:50
  • This is amazing. Ive added code that goes over every logger in system and sets its level to critical. And ive put this code to the top of every file in my app. Still i can see messages about my requests. IMHO its a bug in requests. I mean it makes my program logs unreadable. 9 lines of logs for each request. Commented Jan 1, 2020 at 9:52

2 Answers 2

17

First of all, requests doesn't log anything; only the urllib3 library that requests depends on does. That library only logs messages at INFO and DEBUG levels, so setting the log level to logging.CRITICAL disables all messages already:

urllib3_log = logging.getLogger("urllib3")
urllib3_log.setLevel(logging.CRITICAL)

You could also just disable propagation:

logging.getLogger("urllib3").propagate = False

That's enough to completely disable all logging that the urllib3 library does.

The urllib3 project has installed a NullHandler() handler object on the project root logger, which ensures that the lastResort handler is not used for unhandled messages, even when propagation has been disabled.

That said, if you don't trust that future versions of requests won't use sys.maxint as a log level, and at the same time neglect to set a NullHandler(), then by all means add your own NullHandler() on the project root logger object, and then disable propagation there:

import logging

requests_log = logging.getLogger("requests")
requests_log.addHandler(logging.NullHandler())
requests_log.propagate = False

Now all logging within the requests hierarchy will be directed to the NullHandler() instance, and with propagate set to False logging stops there. You can use this to silence any logger in the hierarchy.

In my opinion, that's overkill, no requests release made so far uses logging, and the project is staffed with capable developers that are unlikely to misconfigure the logging setup if they ever did add logging.

Sign up to request clarification or add additional context in comments.

16 Comments

I'm on python version 2.7.6 (requests 2.12.4, urllib3 1.7.1). Even after I do all these I get log entry per request. There's no indication where these logs are from though.
@chanux: I'm sorry, why are you telling me your versions?
Sorry I pressed enter accidentally :). Any help is appreciated.
@chanux: I updated the answer to be more clear that you'd need to disable logging for urllib3, not requests.
What is the purpose of the line requests_log.addHandler(logging.NullHandler())? The Logging HOWTO recommends to attach a logging.NullHandler to the top-level logger of your library, and only if you don't want your library's users to get your library's event messages of severity logging.WARNING or greater printed to sys.stderr in the absence of a logging configuration in their application. But here this is not the library code, this is the application code. So no need to redirect anything since propagate is False.
|
2

The accepted answer didn't work out for me. But urllib3 docs point out that you can disable the logging with this:

import urllib3
urllib3.disable_warnings()

Source: urllib3 docs

1 Comment

This is a cleaner solution. However, I tend to use requests.packages.urllib3.disable_warnings() when importing strictly requests even though it may produce a Module Not Found in your IDE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.