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.
First of all,
requests
doesn't log anything; only theurllib3
library thatrequests
depends on does. That library only logs messages atINFO
andDEBUG
levels, so setting the log level tologging.CRITICAL
disables all messages already:You could also just disable propagation:
That's enough to completely disable all logging that the
urllib3
library does.The
urllib3
project has installed aNullHandler()
handler object on the project root logger, which ensures that thelastResort
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 usesys.maxint
as a log level, and at the same time neglect to set aNullHandler()
, then by all means add your ownNullHandler()
on the project root logger object, and then disable propagation there:Now all logging within the
requests
hierarchy will be directed to theNullHandler()
instance, and withpropagate
set toFalse
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.