From e8b330f3ba8e4ec10cda294837dddef46da55d0c Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Wed, 3 Sep 2014 15:26:20 +0200 Subject: [PATCH] Strip message for debug logging. Truncated lines --- headphones/request.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/headphones/request.py b/headphones/request.py index 756584ce..1d4f6737 100644 --- a/headphones/request.py +++ b/headphones/request.py @@ -22,7 +22,8 @@ import requests import feedparser import headphones -def request_response(url, method="get", auto_raise=True, whitelist_status_code=None, **kwargs): +def request_response(url, method="get", auto_raise=True, + whitelist_status_code=None, **kwargs): """ Convenient wrapper for `requests.get', which will capture the exceptions and log them. On success, the Response object is returned. In case of a @@ -53,16 +54,19 @@ def request_response(url, method="get", auto_raise=True, whitelist_status_code=N try: response.raise_for_status() except: - logger.debug("Response status code %d is not white listed, raised exception", response.status_code) + logger.debug("Response status code %d is not white " + + "listed, raised exception", response.status_code) raise elif auto_raise: response.raise_for_status() return response except requests.ConnectionError: - logger.error("Unable to connect to remote host. Check if the remote host is up and running.") + logger.error("Unable to connect to remote host. Check if the remote " + + "host is up and running.") except requests.Timeout: - logger.error("Request timed out. The remote host did not respeond timely.") + logger.error("Request timed out. The remote host did not respeond " + + "timely.") except requests.HTTPError as e: if e.response is not None: if e.response.status_code >= 500: @@ -73,24 +77,26 @@ def request_response(url, method="get", auto_raise=True, whitelist_status_code=N # I don't think we will end up here, but for completeness cause = "unknown" - logger.error("Request raise HTTP error with status code %d (%s).", e.response.status_code, cause) + logger.error("Request raise HTTP error with status code %d (%s).", + e.response.status_code, cause) # Some servers return extra information in the result. Try to parse - # it for debugging purpose. Messages are limited to 100 characters, + # it for debugging purpose. Messages are limited to 150 characters, # since it may return the whole page in case of normal web page URLs if headphones.VERBOSE: - if e.response.headers.get('content-type') == 'text/html': + if e.response.headers.get("content-type") == "text/html": soup = BeautifulSoup(e.response.content, "html5lib") message = soup.find("body") message = message.text if message else soup.text + message = message.strip() else: - message = e.response.content + message = e.response.content.strip() if message: # Truncate message if it is too long. - if len(message) > 100: - message = message[:100] + "..." + if len(message) > 150: + message = message[:150] + "..." logger.debug("Server responded with message: %s", message) else: