diff --git a/plugins/module_utils/dnac.py b/plugins/module_utils/dnac.py index 83c4e65728..4e44c11bdf 100644 --- a/plugins/module_utils/dnac.py +++ b/plugins/module_utils/dnac.py @@ -65,9 +65,12 @@ def __init__(self, module): } self.dnac_log = dnac_params.get("dnac_log") - # Check if 'dnac_log_level' in the playbook params. If available, - # convert it to uppercase; otherwise, set it to 'INFO' - self.dnac_log_level = dnac_params.get("dnac_log_level", "INFO").upper() + self.dnac_log_level = dnac_params.get("dnac_log_level") + if self.dnac_log_level is None: + self.dnac_log_level = "INFO" + else: + self.dnac_log_level = self.dnac_log_level.upper() + self.is_valid_log_level() log(str(dnac_params)) self.supported_states = ["merged", "deleted", "replaced", "overridden", "gathered", "rendered", "parsed"] @@ -163,7 +166,6 @@ def log(self, message, level="info", frameIncrement=0): level = level.upper() if ( self.dnac_log - and self.dnac_log_level in ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL') and logging.getLevelName(level) >= logging.getLevelName(self.dnac_log_level) ): message = "Module: " + self.__class__.__name__ + ", " + message @@ -404,6 +406,12 @@ def camel_to_snake_case(self, config): return config return new_config + def is_valid_log_level(self): + valid_log_levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL') + if self.dnac_log_level not in valid_log_levels: + raise ValueError("Invalid log level: 'dnac_log_level:{0}'." + " Expected one of {1}.".format(self.dnac_log_level, valid_log_levels)) + def log(msg, level='info', frameIncrement=0): with open('dnac.log', 'a') as of: