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
This commit is contained in:
Anthony Lee 2014-12-16 12:12:23 -08:00
parent cf83528ae9
commit b71801ca0a
2 changed files with 13 additions and 10 deletions

View File

@ -31,3 +31,5 @@ Changes in Version 1.0.3
correct size. correct size.
* Added support for PEP8 checks with tox. * Added support for PEP8 checks with tox.
* Fixed various typos in the documentation. * Fixed various typos in the documentation.
* Fix duplicate debug log message issue that can occur when multiple client
objects are created.

View File

@ -56,6 +56,8 @@ class HTTPJSONRESTClient(httplib2.Http):
SESSION_COOKIE_NAME = 'Authorization' SESSION_COOKIE_NAME = 'Authorization'
#API_VERSION = 'X-API-Version' #API_VERSION = 'X-API-Version'
#CHRP_VERSION = 'X_HP-CHRP-Client-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): def __init__(self, api_url, insecure=False, http_log_debug=False):
super(HTTPJSONRESTClient, super(HTTPJSONRESTClient,
@ -72,8 +74,6 @@ class HTTPJSONRESTClient(httplib2.Http):
self.force_exception_to_status_code = True self.force_exception_to_status_code = True
#self.disable_ssl_certificate_validation = insecure #self.disable_ssl_certificate_validation = insecure
self._logger = logging.getLogger(__name__)
def set_url(self, api_url): def set_url(self, api_url):
#should be http://<Server:Port>/lhos #should be http://<Server:Port>/lhos
self.api_url = api_url.rstrip('/') self.api_url = api_url.rstrip('/')
@ -86,11 +86,11 @@ class HTTPJSONRESTClient(httplib2.Http):
:type flag: bool :type flag: bool
""" """
self.http_log_debug = flag if not HTTPJSONRESTClient.http_log_debug and flag:
if self.http_log_debug:
ch = logging.StreamHandler() ch = logging.StreamHandler()
self._logger.setLevel(logging.DEBUG) HTTPJSONRESTClient._logger.setLevel(logging.DEBUG)
self._logger.addHandler(ch) HTTPJSONRESTClient._logger.addHandler(ch)
HTTPJSONRESTClient.http_log_debug = True
def authenticate(self, user, password, optional=None): def authenticate(self, user, password, optional=None):
""" """
@ -159,15 +159,16 @@ class HTTPJSONRESTClient(httplib2.Http):
header = ' -H "%s: %s"' % (element, kwargs['headers'][element]) header = ' -H "%s: %s"' % (element, kwargs['headers'][element])
string_parts.append(header) 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: 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): def _http_log_resp(self, resp, body):
if not self.http_log_debug: if not self.http_log_debug:
return return
self._logger.debug("RESP:%s\n", pprint.pformat(resp)) HTTPJSONRESTClient._logger.debug("RESP:%s\n", pprint.pformat(resp))
self._logger.debug("RESP BODY:%s\n", body) HTTPJSONRESTClient._logger.debug("RESP BODY:%s\n", body)
def request(self, *args, **kwargs): def request(self, *args, **kwargs):
""" """