Explain Codes LogoExplain Codes Logo

How do I disable log messages from the Requests library?

python
logging
requests
urllib3
Anton ShumikhinbyAnton Shumikhin·Nov 22, 2024
TLDR

Hush the Requests library chatter by setting the logging threshold:

#importing logging: 'cause silence is golden import logging #Setting the level to ERROR: 'cause we only wake up for the big stuff logging.getLogger("requests").setLevel(logging.ERROR)

This line of code sets the logger for requests to ERROR. Voila! INFO and DEBUG messages are sent into hibernation. Implement this prior to your HTTP request party, and enjoy the quiet.

How to tame urllib3 logs

Should you also be battling the noise from libraries that employ urllib3 under the hood, we've got you covered. Mute urllib3 in a similar fashion:

import logging #urllib3, keep it down, will ya? logging.getLogger('urllib3').setLevel(logging.ERROR)

This specifies that our battle is with the urllib3 monster, notorious for causing excessive logging from the Requests library.

Agh, the logger's offspring are chatty too!

Logger children, much like human ones, at times create a cacophony. They inherit settings and broadcast messages, initiating a log stampede. The propagate attribute comes to the rescue:

import logging #Let's stop any logger family reunions logging.getLogger("requests").propagate = False

A fusion of adjusting logging levels and the propagate attribute is your magic potion against an unwarranted log storm.

I want to hear some loggers, not all!

If fostering an environment of verbose debug logs while muting specific droning loggers is what you seek, then the Logger.manager.loggerDict tool from the logging module is your friend:

import logging #Time to play favorites with loggers for name in logging.Logger.manager.loggerDict: if 'urllib3' in name or 'requests' in name: logging.getLogger(name).setLevel(logging.CRITICAL)

This will scan through all loggers and sends only the ones throwing a requests or urllib3 tantrum to their room.

Adjusting verbosity per development phase

As your code navigates through various development stages, the need to alter log verbosity may arise. Make conditional logging levels work for you:

import logging import os #Giving DEBUG the power during late nights, and ERROR its coffee in the production mornings if os.getenv('DEBUG_MODE'): logging.getLogger("requests").setLevel(logging.DEBUG) else: logging.getLogger("requests").setLevel(logging.ERROR)

This enables you to switch between quiet and verbose modes aided by an environment variable, making transitions from late-night debug sessions to early morning production runs seamless.

Identifying elusive loggers

When loggers decide to play hide-and-seek, and you do not have their explicit names, resort to identifying them post class instantiation or during runtime:

import logging #Unmasking all the mystery loggers after the party has started for logger_name in logging.Logger.manager.loggerDict: print(logger_name)

This gives a roll-call of all active loggers. Silence them selectively or as a group, as per your requirement.

Your one-stop-shop: dictConfig

If dealing with a complex application structure, logging.config.dictConfig might become the lynchpin for precise logger management:

import logging.config LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'loggers': { 'requests': { 'level': 'ERROR', 'propagate': False, }, }, } #Steering the ship of App logs in one go, 'cause we like efficient debugging logging.config.dictConfig(LOGGING)

It allows for centralized command over log rules and regulations.