From cc8a90fd59ca8e23f3fcd5e2b3842e484d24edab Mon Sep 17 00:00:00 2001 From: Sean Yang Date: Tue, 28 Jan 2025 13:32:03 -0800 Subject: [PATCH] Fix log fl_ctx parsing (#3179) Add failure case when attempting to parse bracketed fl_ctx from message ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Quick tests passed locally by running `./runtest.sh`. - [ ] In-line docstrings updated. - [ ] Documentation updated. --- nvflare/fuel/utils/log_utils.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/nvflare/fuel/utils/log_utils.py b/nvflare/fuel/utils/log_utils.py index a603fad0c7..2e15b5ee98 100644 --- a/nvflare/fuel/utils/log_utils.py +++ b/nvflare/fuel/utils/log_utils.py @@ -94,24 +94,32 @@ def format(self, record): record.fl_ctx = "" record.identity = "" message = record.getMessage() + # attempting to parse fl ctx key value pairs "[key0=value0, key1=value1,... ]: " from message 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.get("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 was not fl_ctx format + 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__(