From addce795073914440415039c6dec06a3b1177910 Mon Sep 17 00:00:00 2001 From: Sean Yang Date: Thu, 23 Jan 2025 17:34:06 -0800 Subject: [PATCH] fix fl_ctx parsing bug --- nvflare/fuel/utils/log_utils.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/nvflare/fuel/utils/log_utils.py b/nvflare/fuel/utils/log_utils.py index a603fad0c7..85844407cd 100644 --- a/nvflare/fuel/utils/log_utils.py +++ b/nvflare/fuel/utils/log_utils.py @@ -96,22 +96,29 @@ def format(self, record): message = record.getMessage() fl_ctx_match = re.search(r"\[(.*?)\]: ", message) if fl_ctx_match: - fl_ctx_pairs = { - pair.split("=", 1)[0]: pair.split("=", 1)[1] for pair in fl_ctx_match.group(1).split(", ") - } - record.fl_ctx = fl_ctx_match[0][:-2] - record.identity = fl_ctx_pairs["identity"] # TODO add more values as attributes? - record.msg = message.replace(fl_ctx_match[0], "") - self._style._fmt = self.fmt + try: + fl_ctx_pairs = { + pair.split("=", 1)[0]: pair.split("=", 1)[1] for pair in fl_ctx_match.group(1).split(", ") + } + record.fl_ctx = fl_ctx_match[0][:-2] + record.identity = fl_ctx_pairs["identity"] # TODO add more values as attributes? + record.msg = message.replace(fl_ctx_match[0], "") + self._style._fmt = self.fmt + except: + # found brackets pattern, but invalid fl_ctx + self.remove_empty_placeholders() else: - for placeholder in [ - " %(fl_ctx)s -", - " %(identity)s -", - ]: # TODO generalize this or add default values? - self._style._fmt = self._style._fmt.replace(placeholder, "") + self.remove_empty_placeholders() return super().format(record) + def remove_empty_placeholders(self): + for placeholder in [ + " %(fl_ctx)s -", + " %(identity)s -", + ]: # TODO generalize this or add default values? + self._style._fmt = self._style._fmt.replace(placeholder, "") + class ColorFormatter(BaseFormatter): def __init__(