From 301539982ad54be72017b3c043f0bdc67e836543 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Tue, 3 Oct 2023 00:31:15 +0200 Subject: [PATCH] CommonClient: fix for fix json prints not being logged in UI mode --- CommonClient.py | 9 +++++++-- Utils.py | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CommonClient.py b/CommonClient.py index 3f9065b6738f..154b61b1d577 100644 --- a/CommonClient.py +++ b/CommonClient.py @@ -244,6 +244,7 @@ def __init__(self, server_address: typing.Optional[str], password: typing.Option self.watcher_event = asyncio.Event() self.jsontotextparser = JSONtoTextParser(self) + self.rawjsontotextparser = RawJSONtoTextParser(self) self.update_data_package(network_data_package) # execution @@ -379,9 +380,13 @@ def on_print(self, args: dict): def on_print_json(self, args: dict): if self.ui: + # send copy to UI self.ui.print_json(copy.deepcopy(args["data"])) - text = self.jsontotextparser(args["data"]) - logger.info(text) + + logging.getLogger("FileLog").info(self.rawjsontotextparser(copy.deepcopy(args["data"])), + extra={"NoStream": True}) + logging.getLogger("StreamLog").info(self.jsontotextparser(copy.deepcopy(args["data"])), + extra={"NoFile": True}) def on_package(self, cmd: str, args: dict): """For custom package handling in subclasses.""" diff --git a/Utils.py b/Utils.py index 60b1cdadb7fe..a7499fe690d5 100644 --- a/Utils.py +++ b/Utils.py @@ -446,11 +446,21 @@ def init_logging(name: str, loglevel: typing.Union[str, int] = logging.INFO, wri write_mode, encoding="utf-8-sig") file_handler.setFormatter(logging.Formatter(log_format)) + + class Filter(logging.Filter): + def __init__(self, filter_name, condition): + super().__init__(filter_name) + self.condition = condition + + def filter(self, record: logging.LogRecord) -> bool: + return self.condition(record) + + file_handler.addFilter(Filter("NoStream", lambda record: not getattr(record, "NoFile", False))) root_logger.addHandler(file_handler) if sys.stdout: - root_logger.addHandler( - logging.StreamHandler(sys.stdout) - ) + stream_handler = logging.StreamHandler(sys.stdout) + stream_handler.addFilter(Filter("NoFile", lambda record: not getattr(record, "NoStream", False))) + root_logger.addHandler(stream_handler) # Relay unhandled exceptions to logger. if not getattr(sys.excepthook, "_wrapped", False): # skip if already modified