From 9cfd185676e1bd0d5642c43bb9ee1f857ecd1be4 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Sat, 28 Dec 2024 06:40:05 +1100 Subject: [PATCH] Add option to log non-error output to stdout (#6243) * nit * Add option to log non-error output to stdout - No change to default behaviour - Adds CLI argument: --log-stdout - With this arg present, any logging of a level below logging.ERROR will be sent to stdout instead of stderr --- app/logger.py | 13 ++++++++++++- comfy/cli_args.py | 3 ++- main.py | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/logger.py b/app/logger.py index 527be9fe71f..9e9f84ccf38 100644 --- a/app/logger.py +++ b/app/logger.py @@ -51,7 +51,7 @@ def on_flush(callback): if stderr_interceptor is not None: stderr_interceptor.on_flush(callback) -def setup_logger(log_level: str = 'INFO', capacity: int = 300): +def setup_logger(log_level: str = 'INFO', capacity: int = 300, use_stdout: bool = False): global logs if logs: return @@ -70,4 +70,15 @@ def setup_logger(log_level: str = 'INFO', capacity: int = 300): stream_handler = logging.StreamHandler() stream_handler.setFormatter(logging.Formatter("%(message)s")) + + if use_stdout: + # Only errors and critical to stderr + stream_handler.addFilter(lambda record: not record.levelno < logging.ERROR) + + # Lesser to stdout + stdout_handler = logging.StreamHandler(sys.stdout) + stdout_handler.setFormatter(logging.Formatter("%(message)s")) + stdout_handler.addFilter(lambda record: record.levelno < logging.ERROR) + logger.addHandler(stdout_handler) + logger.addHandler(stream_handler) diff --git a/comfy/cli_args.py b/comfy/cli_args.py index 224c075f019..812798bf88f 100644 --- a/comfy/cli_args.py +++ b/comfy/cli_args.py @@ -122,7 +122,7 @@ class LatentPreviewMethod(enum.Enum): vram_group.add_argument("--novram", action="store_true", help="When lowvram isn't enough.") vram_group.add_argument("--cpu", action="store_true", help="To use the CPU for everything (slow).") -parser.add_argument("--reserve-vram", type=float, default=None, help="Set the amount of vram in GB you want to reserve for use by your OS/other software. By default some amount is reverved depending on your OS.") +parser.add_argument("--reserve-vram", type=float, default=None, help="Set the amount of vram in GB you want to reserve for use by your OS/other software. By default some amount is reserved depending on your OS.") parser.add_argument("--default-hashing-function", type=str, choices=['md5', 'sha1', 'sha256', 'sha512'], default='sha256', help="Allows you to choose the hash function to use for duplicate filename / contents comparison. Default is sha256.") @@ -141,6 +141,7 @@ class LatentPreviewMethod(enum.Enum): parser.add_argument("--multi-user", action="store_true", help="Enables per-user storage.") parser.add_argument("--verbose", default='INFO', const='DEBUG', nargs="?", choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help='Set the logging level') +parser.add_argument("--log-stdout", action="store_true", help="Send normal process output to stdout instead of stderr (default).") # The default built-in provider hosted under web/ DEFAULT_VERSION_STRING = "comfyanonymous/ComfyUI@latest" diff --git a/main.py b/main.py index ccc99fdc44a..95972f73b13 100644 --- a/main.py +++ b/main.py @@ -17,7 +17,7 @@ os.environ['DO_NOT_TRACK'] = '1' -setup_logger(log_level=args.verbose) +setup_logger(log_level=args.verbose, use_stdout=args.log_stdout) def apply_custom_paths(): # extra model paths