Skip to content

Commit

Permalink
SW-3537 improve recursive log prevention (#1801)
Browse files Browse the repository at this point in the history
improve recursive log prevention
  • Loading branch information
Josef-MrBeam authored Sep 18, 2023
1 parent 0cb1b30 commit 3c7e085
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions octoprint_mrbeam/mrb_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, id, ignorePrinter=False, lvl=None):
self.id = id
self.id_short = self._shorten_id(id)
self.my_buffer = []
self.messages_to_log = [] # list of messages to log, to prevent recursive logs
self.messages_to_log = {} # dict of messages to log, to prevent recursive logs
self.recursive_depth = 0
# TODO: this line overrides logging.yaml!!!
if lvl is not None:
Expand Down Expand Up @@ -87,17 +87,20 @@ def log(self, level, msg, *args, **kwargs):
:param terminal_dump: Collect and log a terminal dump. Terminal dumps are also sent to analytics if analytics is not explicitly set to False.
:type kwargs:
"""
if "{} {}".format(self.RECURSIVE_LOG_MESSAGE, msg) in self.messages_to_log:
# we already logged this message, don't log it again
return
msg_hash = self._hash_log(level, msg, args, kwargs)

if msg in self.messages_to_log:
if msg_hash in self.messages_to_log:
# change the log message that this is a recursive call
level = logging.ERROR
msg = "{} {}".format(self.RECURSIVE_LOG_MESSAGE, msg)
kwargs["analytics"] = True
msg_hash = self._hash_log(level, msg, args, kwargs)

self.messages_to_log.append(msg) # to prevent recursive calls
if msg_hash in self.messages_to_log:
# we already logged this message, don't log it again
return

self.messages_to_log[msg_hash] = msg # to prevent recursive calls

try:
if isinstance(msg, unicode):
Expand Down Expand Up @@ -147,8 +150,12 @@ def log(self, level, msg, *args, **kwargs):
print(">>", msg)

# remove the message from the list
index_to_remove = self.messages_to_log.index(msg)
self.messages_to_log.pop(index_to_remove)
if msg_hash in self.messages_to_log:
self.messages_to_log.pop(msg_hash)

def _hash_log(self, *args, **kwargs):
args_tuple = (args, kwargs)
return hash(str(args_tuple))

def _terminal(self, level, msg, *args, **kwargs):
global _printer
Expand Down

0 comments on commit 3c7e085

Please sign in to comment.