From e9c33e1f288c9187f371ceab93116aad8852664d Mon Sep 17 00:00:00 2001 From: Charlie Powell <31997505+cp2004@users.noreply.github.com> Date: Wed, 9 Feb 2022 17:46:13 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=20Add=20`--no-color`=20option=20to?= =?UTF-8?q?=20disable=20colored=20output=20on=20console=20(#4402)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :sparkles: Add `--no-color` option to disable colored output on console Closes #4396 Also removed the setting of INFO logs to white, instead it will be left as default. If there is a light-colored background (such as white) then it becomes unreadable. Can then just use whatever user has configured for their default and not worry about contrast issues. * 🎨 Remove redundant bool wrapper * ⬆️ Update to colorlog 6 That way we get support for NO_COLOR as well: https://no-color.org/ * 💬 Add NO_COLOR to --no-color help Co-authored-by: Gina Häußge --- setup.py | 2 +- src/octoprint/__init__.py | 6 ++++-- src/octoprint/cli/__init__.py | 10 ++++++++++ src/octoprint/cli/server.py | 4 ++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index dcce524e7..1ee9a1ee4 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ bundled_plugins = [ core_deps = [ "cachelib>=0.2,<0.3", "Click>=8.0.3,<9", - "colorlog>=5.0.1,<6", + "colorlog>=6,<7", "emoji>=1.4.2,<2", "feedparser>=6.0.8,<7", "filetype>=1.0.7,<2", diff --git a/src/octoprint/__init__.py b/src/octoprint/__init__.py index bfaee7ea8..5cd8e5a70 100644 --- a/src/octoprint/__init__.py +++ b/src/octoprint/__init__.py @@ -68,6 +68,7 @@ def init_platform( verbosity=0, uncaught_logger=None, uncaught_handler=None, + disable_color=True, safe_mode=False, ignore_blacklist=False, after_preinit_logging=None, @@ -109,6 +110,7 @@ def init_platform( verbosity=verbosity, uncaught_logger=uncaught_logger, uncaught_handler=uncaught_handler, + disable_color=disable_color, ) except Exception as ex: raise FatalStartupError("Could not initialize logging", cause=ex) @@ -252,6 +254,7 @@ def init_logging( verbosity=0, uncaught_logger=None, uncaught_handler=None, + disable_color=True, ): """Sets up logging.""" @@ -272,7 +275,6 @@ def init_logging( "reset": True, "log_colors": { "DEBUG": "cyan", - "INFO": "white", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "bold_red", @@ -286,7 +288,7 @@ def init_logging( "console": { "class": "octoprint.logging.handlers.OctoPrintStreamHandler", "level": "DEBUG", - "formatter": "colored", + "formatter": "simple" if disable_color else "colored", "stream": "ext://sys.stdout", }, "file": { diff --git a/src/octoprint/cli/__init__.py b/src/octoprint/cli/__init__.py index 827396249..57f753c09 100644 --- a/src/octoprint/cli/__init__.py +++ b/src/octoprint/cli/__init__.py @@ -62,6 +62,7 @@ def init_platform_for_cli(ctx): get_ctx_obj_option(ctx, "configfile", None), overlays=get_ctx_obj_option(ctx, "overlays", None), safe_mode=True, + disable_color=get_ctx_obj_option(ctx, "no_color", False), ) ( @@ -236,6 +237,15 @@ def standard_options(hidden=False): expose_value=False, help="Enable safe mode; disables all third party plugins.", ), + factory( + "--no-color", + "no_color", + is_flag=True, + callback=set_ctx_obj_option, + is_eager=True, + expose_value=False, + help="Disable colored console output. Alternatively set the NO_COLOR environment variable to a value of 1.", + ), ] return bulk_options(options) diff --git a/src/octoprint/cli/server.py b/src/octoprint/cli/server.py index dcefd9d07..0a234eace 100644 --- a/src/octoprint/cli/server.py +++ b/src/octoprint/cli/server.py @@ -31,6 +31,7 @@ def run_server( ignore_blacklist, octoprint_daemon=None, overlays=None, + disable_color=False, ): """Initializes the environment and starts up the server.""" @@ -120,6 +121,7 @@ def run_server( ignore_blacklist=ignore_blacklist, after_safe_mode=log_startup, after_environment_detector=log_register_rollover, + disable_color=disable_color, ) ( settings, @@ -307,6 +309,7 @@ def serve_command(ctx, **kwargs): safe_mode = "flag" if get_value("safe_mode") else None ignore_blacklist = get_value("ignore_blacklist") overlays = get_value("overlays") + no_color = get_value("no_color") if v4 and not host: host = "0.0.0.0" @@ -324,6 +327,7 @@ def serve_command(ctx, **kwargs): safe_mode, ignore_blacklist, overlays=overlays, + disable_color=no_color, )