Skip to content

Commit

Permalink
stateengine plugin: improve logging (e.g. for handling released_by), …
Browse files Browse the repository at this point in the history
…introducing internal prefix for log messages
  • Loading branch information
onkelandy committed Sep 23, 2024
1 parent 61ffec0 commit 4e3cb99
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
8 changes: 6 additions & 2 deletions stateengine/StateEngineItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ def update_can_release_list():

self.__logger.info("".ljust(80, "_"))
self.__logger.info("Handling released_by attributes")
self.__logger.increase_indent("Handling released_by... ")
can_release = {}
all_released_by = {}
skip_copy = True
Expand Down Expand Up @@ -1037,7 +1038,7 @@ def update_can_release_list():
self.__release_info = {new_state.id: _can_release_list}
_key_releasedby = ['{}'.format(new_state.id), 'releasedby']
self.update_webif(_key_releasedby, _can_release_list)

self.__logger.increase_indent("")
self.__logger.info("".ljust(80, "_"))
return all_released_by

Expand Down Expand Up @@ -1333,6 +1334,7 @@ def __reorder_states(self, init=True):
_reordered_states = []
self.__logger.info("".ljust(80, "_"))
self.__logger.info("Recalculating state order. Current order: {}", self.__states)
self.__logger.increase_indent()
_copied_states = {}
_add_order = 0
_changed_orders = []
Expand Down Expand Up @@ -1398,6 +1400,7 @@ def __reorder_states(self, init=True):
else:
_reorder_webif[state.id] = self.__webif_infos[state.id]
self.__webif_infos = _reorder_webif
self.__logger.decrease_indent()
self.__logger.info("Recalculated state order. New order: {}", self.__states)
self.__logger.info("".ljust(80, "_"))

Expand Down Expand Up @@ -1895,6 +1898,7 @@ def update_can_release_list():

self.__logger.info("".ljust(80, "_"))
self.__logger.info("Initializing released_by attributes")
self.__logger.increase_indent()
can_release = {}
state_dict = {state.id: state for state in self.__states}
for state in self.__states:
Expand All @@ -1920,7 +1924,7 @@ def update_can_release_list():
self.__config_issues.update({state.id: {'issue': _issuelist, 'attribute': 'se_released_by'}})
state.update_releasedby_internal(_convertedlist)
self.__update_can_release(can_release, state)

self.__logger.decrease_indent()
self.__logger.info("".ljust(80, "_"))

# log item data
Expand Down
18 changes: 11 additions & 7 deletions stateengine/StateEngineLogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def __init__(self, item, manual=False):
self.__name = 'stateengine.{}'.format(item.property.path)
self.__section = item.property.path.replace(".", "_").replace("/", "")
self.__indentlevel = 0
self.__indentprefix = ""
if manual:
self.__log_level_as_num = 2
else:
Expand All @@ -151,7 +152,10 @@ def update_logfile(self):
# Increase indentation level
# by: number of levels to increase
def increase_indent(self, by=1):
self.__indentlevel += by
if isinstance(by, int):
self.__indentlevel += by
else:
self.__indentprefix = by

# Decrease indentation level
# by: number of levels to decrease
Expand All @@ -170,7 +174,7 @@ def log(self, level, text, *args):
indent = "\t" * self.__indentlevel
if args:
text = text.format(*args)
logtext = "{0}{1} {2}\r\n".format(datetime.datetime.now(), indent, text)
logtext = "{0} {1}{2}{3}\r\n".format(datetime.datetime.now(), self.__indentprefix, indent, text)
try:
with open(self.__filename, mode="a", encoding="utf-8") as f:
f.write(logtext)
Expand All @@ -194,7 +198,7 @@ def header(self, text):
def info(self, text, *args):
self.log(1, text, *args)
indent = "\t" * self.__indentlevel
text = '{}{}'.format(indent, text)
text = '{}{}{}'.format(self.__indentprefix, indent, text)
if args:
text = text.format(*args)
self.logger.info(text)
Expand All @@ -205,7 +209,7 @@ def info(self, text, *args):
def debug(self, text, *args):
self.log(2, text, *args)
indent = "\t" * self.__indentlevel
text = '{}{}'.format(indent, text)
text = '{}{}{}'.format(self.__indentprefix, indent, text)
if args:
text = text.format(*args)
self.logger.debug(text)
Expand All @@ -216,7 +220,7 @@ def debug(self, text, *args):
def develop(self, text, *args):
self.log(3, "DEV: " + text, *args)
indent = "\t" * self.__indentlevel
text = '{}{}'.format(indent, text)
text = '{}{}{}'.format(self.__indentprefix, indent, text)
if args:
text = text.format(*args)
self.logger.log(StateEngineDefaults.VERBOSE, text)
Expand All @@ -228,7 +232,7 @@ def develop(self, text, *args):
def warning(self, text, *args):
self.log(1, "WARNING: " + text, *args)
indent = "\t" * self.__indentlevel
text = '{}{}'.format(indent, text)
text = '{}{}{}'.format(self.__indentprefix, indent, text)
if args:
text = text.format(*args)
self.logger.warning(text)
Expand All @@ -240,7 +244,7 @@ def warning(self, text, *args):
def error(self, text, *args):
self.log(1, "ERROR: " + text, *args)
indent = "\t" * self.__indentlevel
text = '{}{}'.format(indent, text)
text = '{}{}{}'.format(self.__indentprefix, indent, text)
if args:
text = text.format(*args)
self.logger.error(text)
Expand Down

0 comments on commit 4e3cb99

Please sign in to comment.