Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

item.py: allow log level to be set by eval expression #681

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions lib/item/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ def __init__(self, smarthome, parent, path, config, items_instance=None):
self._on_change_dest_var_unexp = [] # -> KEY_ON_CHANGE destination var (with unexpanded item reference)
self._log_change = None
self._log_change_logger = None
self._log_level_attrib = None
self._log_level = None
self._log_level_name = None
self._log_mapping = {}
Expand Down Expand Up @@ -420,18 +421,8 @@ def __init__(self, smarthome, parent, path, config, items_instance=None):

elif attr in [KEY_LOG_LEVEL]:
if value != '':
level = value.upper()
level_name = level
if Utils.is_int(level):
level = int(level)
level_name = logging.getLevelName(level)
if logging.getLevelName(level) == 'Level ' + str(level):
logger.warning(f"Item {self._path}: Invalid loglevel '{value}' defined in attribute '{KEY_LOG_LEVEL}' - Level 'INFO' will be used instead")
setattr(self, '_log_level_name', 'INFO')
setattr(self, '_log_level', logging.getLevelName('INFO'))
else:
setattr(self, '_log_level_name', level_name)
setattr(self, '_log_level', logging.getLevelName(level_name))
setattr(self, '_log_level_attrib', value)

elif attr in [KEY_LOG_CHANGE]:
if value != '':
setattr(self, '_log_change', value)
Expand Down Expand Up @@ -2227,7 +2218,7 @@ def _log_build_text(self, value, caller, source=None, dest=None):
#logger.warning(f"self._log_text: {self._log_text}, type={type(self._log_text)}")
txt = eval(f"f'{self._log_text}'")
except Exception as e:
logger.error(f"{id}: Invalid log_text template ' {self._log_text}' - (Exception: {e})")
logger.error(f"{id}: Invalid log_text template '{self._log_text}' - (Exception: {e})")
txt = self._log_text
return txt

Expand Down Expand Up @@ -2312,6 +2303,24 @@ def _log_on_change(self, value, caller, source=None, dest=None):
# if dest is not None:
# log_dst += ', dest: ' + dest
#self._log_change_logger.log(self._log_level, "Item Change: {} = {} - caller: {}{}{}".format(self._path, value, caller, log_src, log_dst))
try:
val = self._log_level_attrib.replace("'", '"')
log_level = eval(f"f'{val}'")
except Exception as e:
log_level = self._log_level_attrib
logger.error(f"{id}: Invalid log_level template '{log_level}' - (Exception: {e})")
level = log_level.upper()
level_name = level
if Utils.is_int(level):
level = int(level)
level_name = logging.getLevelName(level)
if logging.getLevelName(level) == 'Level ' + str(level):
logger.warning(f"Item {self._path}: Invalid loglevel '{log_level}' defined in attribute '{KEY_LOG_LEVEL}' - Level 'INFO' will be used instead")
setattr(self, '_log_level_name', 'INFO')
setattr(self, '_log_level', logging.getLevelName('INFO'))
else:
setattr(self, '_log_level_name', level_name)
setattr(self, '_log_level', logging.getLevelName(level_name))
self._log_change_logger.log(self._log_level, txt)


Expand Down
Loading