簡體   English   中英

如何獲取 Python 請求庫所做的每次重試嘗試的日志?

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

我正在使用以下代碼在 python 請求中實現重試機制。

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')

它工作得很好,但是這一切都是悄無聲息地發生的。 我想要的是在進行重試嘗試時得到通知,如果可能的話,為什么會發生重試。 每次重試我都想要這樣的東西。

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

是否可以?

您可以編寫自己的 Class 來添加應該從重試繼承的日志。

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>)

我發現的最佳選擇是增加 urllib 的日志記錄:

    'urllib3.util.retry':
    level: DEBUG
    handlers: [console]
    propagate: no

Output 是:

2023-01-20 09:

59:41.018 | DEBUG    | logging:callHandlers:1706 - Starting new HTTP connection (1): x.x.x.x:8001
2023-01-20 09:59:48.263 | DEBUG    | logging:callHandlers:1706 - Incremented Retry for (url='/path/'): LogRetry(total=4, connect=None, read=None, redirect=None, status=None)
2023-01-20 09:59:48.264 | WARNING  | logging:callHandlers:1706 - Retrying (LogRetry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x28894bf10>, 'Connection to x.x.x.x timed out. (connect timeout=5)')': /path/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM