From b71801ca0a1fd5131116b9edb3933c4394fa0af1 Mon Sep 17 00:00:00 2001 From: Anthony Lee Date: Tue, 16 Dec 2014 12:12:23 -0800 Subject: [PATCH] Fixing duplicate debug log messages Fixed a bug where multiple loggers would be created if the LeftHand client was initialized multiple times. Debug messages would then start repeating and making debugging difficult. This fix is similar to the fix in the 3PAR client. Change-Id: Ia1af8e608f5d703931b87223e46dc398cd715484 --- docs/changelog.rst | 2 ++ hplefthandclient/http.py | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index c914fd7..15383cf 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -31,3 +31,5 @@ Changes in Version 1.0.3 correct size. * Added support for PEP8 checks with tox. * Fixed various typos in the documentation. +* Fix duplicate debug log message issue that can occur when multiple client + objects are created. diff --git a/hplefthandclient/http.py b/hplefthandclient/http.py index 4d26dae..ddbaf8e 100644 --- a/hplefthandclient/http.py +++ b/hplefthandclient/http.py @@ -56,6 +56,8 @@ class HTTPJSONRESTClient(httplib2.Http): SESSION_COOKIE_NAME = 'Authorization' #API_VERSION = 'X-API-Version' #CHRP_VERSION = 'X_HP-CHRP-Client-Version' + http_log_debug = False + _logger = logging.getLogger(__name__) def __init__(self, api_url, insecure=False, http_log_debug=False): super(HTTPJSONRESTClient, @@ -72,8 +74,6 @@ class HTTPJSONRESTClient(httplib2.Http): self.force_exception_to_status_code = True #self.disable_ssl_certificate_validation = insecure - self._logger = logging.getLogger(__name__) - def set_url(self, api_url): #should be http:///lhos self.api_url = api_url.rstrip('/') @@ -86,11 +86,11 @@ class HTTPJSONRESTClient(httplib2.Http): :type flag: bool """ - self.http_log_debug = flag - if self.http_log_debug: + if not HTTPJSONRESTClient.http_log_debug and flag: ch = logging.StreamHandler() - self._logger.setLevel(logging.DEBUG) - self._logger.addHandler(ch) + HTTPJSONRESTClient._logger.setLevel(logging.DEBUG) + HTTPJSONRESTClient._logger.addHandler(ch) + HTTPJSONRESTClient.http_log_debug = True def authenticate(self, user, password, optional=None): """ @@ -159,15 +159,16 @@ class HTTPJSONRESTClient(httplib2.Http): header = ' -H "%s: %s"' % (element, kwargs['headers'][element]) string_parts.append(header) - self._logger.debug("\nREQ: %s\n" % "".join(string_parts)) + HTTPJSONRESTClient._logger.debug("\nREQ: %s\n" % "".join(string_parts)) if 'body' in kwargs: - self._logger.debug("REQ BODY: %s\n" % (kwargs['body'])) + HTTPJSONRESTClient._logger.debug("REQ BODY: %s\n" + % (kwargs['body'])) def _http_log_resp(self, resp, body): if not self.http_log_debug: return - self._logger.debug("RESP:%s\n", pprint.pformat(resp)) - self._logger.debug("RESP BODY:%s\n", body) + HTTPJSONRESTClient._logger.debug("RESP:%s\n", pprint.pformat(resp)) + HTTPJSONRESTClient._logger.debug("RESP BODY:%s\n", body) def request(self, *args, **kwargs): """