From 9bd90843817c1bf5390b8bd2351195bb7e109664 Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Sat, 6 Jul 2024 10:24:24 +0900 Subject: [PATCH] fix: add functionality to hook the logs generated by the logging module. - Fix the issue where messages generated by the logging module are not being saved to a file and import failures are not being recognized. --- glob/manager_core.py | 2 +- prestartup_script.py | 46 +++++++++++++++++++++++++++++++++----------- pyproject.toml | 2 +- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/glob/manager_core.py b/glob/manager_core.py index 3f1bc48f..a41a3ec4 100644 --- a/glob/manager_core.py +++ b/glob/manager_core.py @@ -23,7 +23,7 @@ sys.path.append(glob_path) import cm_global from manager_util import * -version = [2, 44, 1] +version = [2, 44, 2] version_str = f"V{version[0]}.{version[1]}" + (f'.{version[2]}' if len(version) > 2 else '') diff --git a/prestartup_script.py b/prestartup_script.py index 856d4e6d..d5514d57 100644 --- a/prestartup_script.py +++ b/prestartup_script.py @@ -9,7 +9,7 @@ import locale import platform import json import ast - +import logging glob_path = os.path.join(os.path.dirname(__file__), "glob") sys.path.append(glob_path) @@ -202,6 +202,7 @@ try: is_start_mode = True + class ComfyUIManagerLogger: def __init__(self, is_stdout): self.is_stdout = is_stdout @@ -250,7 +251,7 @@ try: else: self.sync_write(message) - def sync_write(self, message): + def sync_write(self, message, file_only=False): with log_lock: timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')[:-3] if self.last_char != '\n': @@ -260,15 +261,16 @@ try: log_file.flush() self.last_char = message if message == '' else message[-1] - with std_log_lock: - if self.is_stdout: - write_stdout(message) - original_stdout.flush() - terminal_hook.write_stderr(message) - else: - write_stderr(message) - original_stderr.flush() - terminal_hook.write_stdout(message) + if not file_only: + with std_log_lock: + if self.is_stdout: + write_stdout(message) + original_stdout.flush() + terminal_hook.write_stderr(message) + else: + write_stderr(message) + original_stderr.flush() + terminal_hook.write_stdout(message) def flush(self): log_file.flush() @@ -305,6 +307,28 @@ try: else: sys.stdout.close_log = lambda: None + + class LoggingHandler(logging.Handler): + def emit(self, record): + global is_start_mode + + message = record.getMessage() + + if is_start_mode: + match = re.search(pat_import_fail, message) + if match: + import_failed_extensions.add(match.group(1)) + + if 'Starting server' in message: + is_start_mode = False + + if enable_file_logging: + sys.stderr.sync_write(message+'\n', file_only=True) + + + logging.getLogger().addHandler(LoggingHandler()) + + except Exception as e: print(f"[ComfyUI-Manager] Logging failed: {e}") diff --git a/pyproject.toml b/pyproject.toml index 5e95eaf4..4913c850 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "comfyui-manager" description = "ComfyUI-Manager provides features to install and manage custom nodes for ComfyUI, as well as various functionalities to assist with ComfyUI." -version = "2.44.1" +version = "2.44.2" license = "LICENSE" dependencies = ["GitPython", "PyGithub", "matrix-client==0.4.0", "transformers", "huggingface-hub>0.20", "typer", "rich", "typing-extensions"]