diff --git a/hpe3parclient/client.py b/hpe3parclient/client.py index f9b8b3a9..8b441cc6 100644 --- a/hpe3parclient/client.py +++ b/hpe3parclient/client.py @@ -298,15 +298,20 @@ def getWsApiVersion(self): self.http.set_url(self.api_url) def debug_rest(self, flag): - """This is useful for debugging requests to 3PAR. + """Enable/disable log output for the client - :param flag: set to True to enable debugging + By default logs are disabled, even for error messages, enabling debug + mode will enable http and ssh logs at debug level. + + This is useful for debugging all requests to 3PAR. + + :param flag: set to True to enable logs, False to disable. :type flag: bool """ self.http.set_debug_flag(flag) - if self.ssh: - self.ssh.set_debug_flag(flag) + # Call method using the class as self.ssh is not created with instance + ssh.HPE3PARSSHClient.set_debug_flag(flag) def login(self, username, password, optional=None): """This authenticates against the 3PAR wsapi server and creates a diff --git a/hpe3parclient/http.py b/hpe3parclient/http.py index 138a97f9..c826a0a5 100644 --- a/hpe3parclient/http.py +++ b/hpe3parclient/http.py @@ -59,6 +59,8 @@ class HTTPJSONRESTClient(object): SESSION_COOKIE_NAME = 'X-Hp3Par-Wsapi-Sessionkey' http_log_debug = False _logger = logging.getLogger(__name__) + _logger.setLevel(logging.INFO) + _log_handler = None # Retry constants retry_exceptions = (exceptions.HTTPServiceUnavailable, @@ -86,19 +88,28 @@ def set_url(self, api_url): # should be http:///api/v1 self.api_url = api_url.rstrip('/') - def set_debug_flag(self, flag): - """ - This turns on/off http request/response debugging output to console + @classmethod + def set_debug_flag(cls, flag): + """This turns on/off http request/response logs + + By default logs are disabled, even for error messages, enabling debug + mode will enable http logs at debug level. :param flag: Set to True to enable debugging output :type flag: bool """ - if not HTTPJSONRESTClient.http_log_debug and flag: - ch = logging.StreamHandler() - HTTPJSONRESTClient._logger.setLevel(logging.DEBUG) - HTTPJSONRESTClient._logger.addHandler(ch) - HTTPJSONRESTClient.http_log_debug = True + flag = bool(flag) # In case we don't receive a bool instance + if flag != cls.http_log_debug: + if flag: + if cls._log_handler is None: + cls._log_handler = logging.StreamHandler() + cls._logger.addHandler(cls._log_handler) + cls._logger.setLevel(logging.DEBUG) + else: + cls._logger.setLevel(logging.INFO) + cls._logger.removeHandler(cls._log_handler) + cls.http_log_debug = flag def authenticate(self, user, password, optional=None): """ diff --git a/hpe3parclient/ssh.py b/hpe3parclient/ssh.py index 5ce91878..2c2717d1 100644 --- a/hpe3parclient/ssh.py +++ b/hpe3parclient/ssh.py @@ -55,6 +55,7 @@ class HPE3PARSSHClient(object): log_debug = False _logger = logging.getLogger(__name__) _logger.setLevel(logging.INFO) + _log_handler = None def __init__(self, ip, login, password, port=22, conn_timeout=None, privatekey=None, @@ -151,19 +152,28 @@ def close(self): if self.ssh: self.ssh.close() - def set_debug_flag(self, flag): - """ - This turns on ssh debugging output to console + @classmethod + def set_debug_flag(cls, flag): + """This turns on/off log output for ssh commands. + + By default logs are disabled, even for error messages, enabling debug + mode will enable ssh logs at debug level. - :param flag: Set to True to enable debugging output + :param flag: Whether we want to have logs or not :type flag: bool """ - if not HPE3PARSSHClient.log_debug and flag: - ch = logging.StreamHandler() - self._logger.setLevel(logging.DEBUG) - self._logger.addHandler(ch) - HPE3PARSSHClient.log_debug = True + flag = bool(flag) # In case we don't receive a bool instance + if flag != cls.log_debug: + if flag: + if cls._log_handler is None: + cls._log_handler = logging.StreamHandler() + cls._logger.addHandler(cls._log_handler) + cls._logger.setLevel(logging.DEBUG) + else: + cls._logger.setLevel(logging.INFO) + cls._logger.removeHandler(cls._log_handler) + cls.log_debug = flag @staticmethod def sanitize_cert(output_list):