fix: respect NO_COLOR env var in logging format

ANSI escape codes (\033[92m green, \033[0m reset) were hardcoded into the
logging format string, producing garbled output when piped to files or when
the NO_COLOR env var is set (https://no-color.org/).

Now checks NO_COLOR and emits a plain format string when set, matching the
existing convention in setup_wizard.py.

Fixes #29799
This commit is contained in:
michaelxer 2026-06-07 06:38:24 +08:00
parent 68d67212cd
commit 161662dfaa

View file

@ -249,8 +249,12 @@ if json_logs:
handler.setFormatter(JsonFormatter())
_setup_json_exception_handlers(JsonFormatter())
else:
if os.environ.get("NO_COLOR") is None:
_format_str = "\033[92m%(asctime)s - %(name)s:%(levelname)s\033[0m: %(filename)s:%(lineno)s - %(message)s"
else:
_format_str = "%(asctime)s - %(name)s:%(levelname)s: %(filename)s:%(lineno)s - %(message)s"
formatter = logging.Formatter(
"\033[92m%(asctime)s - %(name)s:%(levelname)s\033[0m: %(filename)s:%(lineno)s - %(message)s",
_format_str,
datefmt="%H:%M:%S",
)