From 953117efa1af328ec6eec2e8c4724252f1207cf8 Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Wed, 31 Dec 2025 18:52:44 +0800 Subject: [PATCH] feat: add logging setup and standalone mode detection to LoRA Manager - Initialize logging configuration via `setup_logging()` when not in standalone mode - Detect standalone mode using environment variables `LORA_MANAGER_STANDALONE` and `HF_HUB_DISABLE_TELEMETRY` - Remove redundant `STANDALONE_MODE` variable that previously checked `sys.modules` --- py/lora_manager.py | 12 +++++++++--- py/utils/logging_config.py | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 py/utils/logging_config.py diff --git a/py/lora_manager.py b/py/lora_manager.py index b6b1fccd..fa1eaadb 100644 --- a/py/lora_manager.py +++ b/py/lora_manager.py @@ -2,6 +2,15 @@ import asyncio import sys import os import logging +from .utils.logging_config import setup_logging + +# Check if we're in standalone mode +standalone_mode = os.environ.get("LORA_MANAGER_STANDALONE", "0") == "1" or os.environ.get("HF_HUB_DISABLE_TELEMETRY", "0") == "0" + +# Only setup logging prefix if not in standalone mode +if not standalone_mode: + setup_logging() + from server import PromptServer # type: ignore from .config import config @@ -21,9 +30,6 @@ from .middleware.csp_middleware import relax_csp_for_remote_media logger = logging.getLogger(__name__) -# Check if we're in standalone mode -STANDALONE_MODE = 'nodes' not in sys.modules - HEADER_SIZE_LIMIT = 16384 diff --git a/py/utils/logging_config.py b/py/utils/logging_config.py new file mode 100644 index 00000000..52205c7f --- /dev/null +++ b/py/utils/logging_config.py @@ -0,0 +1,26 @@ +import logging +import os + +def setup_logging(): + """ + Sets up a global log record factory that prepends '[LoRA-Manager]' to all logs + generated by this extension. + """ + # project_root should be the parent of the directory containing this file (py/utils/logging_config.py) + # So project_root is ComfyUI-Lora-Manager/ + project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + prefix = "[LoRA-Manager] " + + old_factory = logging.getLogRecordFactory() + def factory(*args, **kwargs): + record = old_factory(*args, **kwargs) + + # Check if the log is coming from our extension + # We use pathname to verify if it's within our project directory + if record.pathname and os.path.abspath(record.pathname).startswith(project_root): + if isinstance(record.msg, str) and not record.msg.startswith(prefix): + record.msg = f"{prefix}{record.msg}" + + return record + + logging.setLogRecordFactory(factory)