From e91662e7849aed594abf425e88660cb09f502926 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Fri, 30 Aug 2024 12:46:37 -0400 Subject: [PATCH] Get logs endpoint & system_stats additions (#4690) * Add route for getting output logs * Include ComfyUI version * Move to own function * Changed to memory logger * Unify logger setup logic * Fix get version git fallback --------- Co-authored-by: pythongosssss <125205205+pythongosssss@users.noreply.github.com> --- api_server/routes/internal/internal_routes.py | 4 +++ app/logger.py | 31 +++++++++++++++++++ comfy/cli_args.py | 7 ----- main.py | 4 +++ server.py | 25 +++++++++++++-- 5 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 app/logger.py diff --git a/api_server/routes/internal/internal_routes.py b/api_server/routes/internal/internal_routes.py index 2234b311..8c46215f 100644 --- a/api_server/routes/internal/internal_routes.py +++ b/api_server/routes/internal/internal_routes.py @@ -2,6 +2,7 @@ from aiohttp import web from typing import Optional from folder_paths import models_dir, user_directory, output_directory from api_server.services.file_service import FileService +import app.logger class InternalRoutes: ''' @@ -31,6 +32,9 @@ class InternalRoutes: except Exception as e: return web.json_response({"error": str(e)}, status=500) + @self.routes.get('/logs') + async def get_logs(request): + return web.json_response(app.logger.get_logs()) def get_app(self): if self._app is None: diff --git a/app/logger.py b/app/logger.py new file mode 100644 index 00000000..17662c9e --- /dev/null +++ b/app/logger.py @@ -0,0 +1,31 @@ +import logging +from logging.handlers import MemoryHandler +from collections import deque + +logs = None +formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") + + +def get_logs(): + return "\n".join([formatter.format(x) for x in logs]) + + +def setup_logger(verbose: bool = False, capacity: int = 300): + global logs + if logs: + return + + # Setup default global logger + logger = logging.getLogger() + logger.setLevel(logging.DEBUG if verbose else logging.INFO) + + stream_handler = logging.StreamHandler() + stream_handler.setFormatter(logging.Formatter("%(message)s")) + logger.addHandler(stream_handler) + + # Create a memory handler with a deque as its buffer + logs = deque(maxlen=capacity) + memory_handler = MemoryHandler(capacity, flushLevel=logging.INFO) + memory_handler.buffer = logs + memory_handler.setFormatter(formatter) + logger.addHandler(memory_handler) diff --git a/comfy/cli_args.py b/comfy/cli_args.py index c98d4dfa..3a0a26d0 100644 --- a/comfy/cli_args.py +++ b/comfy/cli_args.py @@ -179,10 +179,3 @@ if args.windows_standalone_build: if args.disable_auto_launch: args.auto_launch = False - -import logging -logging_level = logging.INFO -if args.verbose: - logging_level = logging.DEBUG - -logging.basicConfig(format="%(message)s", level=logging_level) diff --git a/main.py b/main.py index 9bd98514..3db28e1f 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,10 @@ import importlib.util import folder_paths import time from comfy.cli_args import args +from app.logger import setup_logger + + +setup_logger(verbose=args.verbose) def execute_prestartup_script(): diff --git a/server.py b/server.py index d8a9916b..26bfcdd9 100644 --- a/server.py +++ b/server.py @@ -30,7 +30,7 @@ from app.user_manager import UserManager from model_filemanager import download_model, DownloadModelStatus from typing import Optional from api_server.routes.internal.internal_routes import InternalRoutes - +import torch class BinaryEventTypes: PREVIEW_IMAGE = 1 @@ -42,6 +42,21 @@ async def send_socket_catch_exception(function, message): except (aiohttp.ClientError, aiohttp.ClientPayloadError, ConnectionResetError) as err: logging.warning("send error: {}".format(err)) +def get_comfyui_version(): + comfyui_version = "unknown" + repo_path = os.path.dirname(os.path.realpath(__file__)) + try: + import pygit2 + repo = pygit2.Repository(repo_path) + comfyui_version = repo.describe(describe_strategy=pygit2.GIT_DESCRIBE_TAGS) + except Exception: + try: + import subprocess + comfyui_version = subprocess.check_output(["git", "describe", "--tags"], cwd=repo_path) + except Exception as e: + logging.warning(f"Failed to get ComfyUI version: {e}") + return comfyui_version.strip() + @web.middleware async def cache_control(request: web.Request, handler): response: web.Response = await handler(request) @@ -401,16 +416,20 @@ class PromptServer(): return web.json_response(dt["__metadata__"]) @routes.get("/system_stats") - async def get_queue(request): + async def system_stats(request): device = comfy.model_management.get_torch_device() device_name = comfy.model_management.get_torch_device_name(device) vram_total, torch_vram_total = comfy.model_management.get_total_memory(device, torch_total_too=True) vram_free, torch_vram_free = comfy.model_management.get_free_memory(device, torch_free_too=True) + system_stats = { "system": { "os": os.name, + "comfyui_version": get_comfyui_version(), "python_version": sys.version, - "embedded_python": os.path.split(os.path.split(sys.executable)[0])[1] == "python_embeded" + "pytorch_version": torch.version.__version__, + "embedded_python": os.path.split(os.path.split(sys.executable)[0])[1] == "python_embeded", + "argv": sys.argv }, "devices": [ {