Skip to content

Commit

Permalink
Merge pull request #681 from onkelandy/log_level
Browse files Browse the repository at this point in the history
item.py: allow log level to be set by eval expression
  • Loading branch information
Morg42 authored Oct 16, 2024
2 parents ac8992e + 430f39c commit f8a5aa2
Showing 1 changed file with 22 additions and 13 deletions.
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

0 comments on commit f8a5aa2

Please sign in to comment.