Search code examples
pythonpython-requestsurllib3

How to get log of every retry attempt made by Python Requests Library?


I'm using following code to implement retry mechanism in python requests.

from requests import Session
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

s = Session()
retries = Retry(total=5,
                raise_on_redirect=True, 
                raise_on_status=True,
                backoff_factor=1,
                status_forcelist=[ 500, 502, 503, 504 ])
s.mount('http://', HTTPAdapter(max_retries=retries))
s.mount('https://', HTTPAdapter(max_retries=retries))
response=s.get('https://example.com')

It is working perfectly, however all this happens silently. What I want is to get informed whenever a retry attempt is made and if possible why that retry was happened. I want something like this for every retry attempt.

print(Exception.__class__)
print('Retrying after' + x + 'seconds')

Is it possible?


Solution

  • You can write your own Class to add logs it should be inherited from the Retry.

    class LogRetry(Retry):
      """
         Adding extra logs before making a retry request     
      """      
      def __init__(self, *args, **kwargs):
        logger.info(f'<add your logs here>')
        super().__init__(*args, **kwargs)
    
    retries = LogRetry(<your args>)