Files
TanishqandHarry f856d9c643 fix(log): keep WebUI and API log lines when output is redirected on Windows
When the output of the WebUI or the API server goes to a file or a pipe
on Windows, as with "webui.bat > webui.log 2>&1", Python writes stdout
and stderr in the ANSI code page. The terminal log sink then raises
UnicodeEncodeError for every record with a character outside that code
page, and Loguru drops the record and prints "Logging error in Loguru
Handler" in its place. On English Windows (cp1252) each render loses
the "① video" to "⑤ font" lines of generate_video() and every record
with Chinese text, such as the task parameters and the video script.
On Chinese Windows (cp936) the records with emoji or with letters that
GBK lacks, such as German ä, ö and ß, are lost, and the log is written
in GBK.

cli.py already switched both streams to UTF-8, but only for the CLI.
The helper moves to app.utils.logging_utils as force_utf8_console(),
and app/config/__init__.py calls it before config.py logs anything,
which covers the API, the WebUI and the CLI. cli.py and the mpt
launcher call the shared helper.
2026-10-31 17:56:51 +08:00

47 lines
1.2 KiB
Python

import sys
from app.utils.logging_utils import (
LOG_LEVELS,
configure_terminal_logger,
force_utf8_console,
set_logging_options,
)
# The API, the WebUI and the CLI all import this package first. Switch the
# console streams before config.py logs anything while it loads.
force_utf8_console()
from app.config import config # noqa: E402
def __init_logger():
# _log_file = utils.storage_dir("logs/server.log")
_lvl = str(config.diagnostics.get("log_level", config.log_level)).strip().upper()
if _lvl not in LOG_LEVELS:
_lvl = str(config.log_level).strip().upper()
if _lvl not in LOG_LEVELS:
_lvl = "DEBUG"
# 分别校验两个选项:非法追踪值不能降低用户已有的日志等级。
_trace = config.diagnostics.get("log_requests", False)
set_logging_options(_lvl, _trace if isinstance(_trace, bool) else False)
configure_terminal_logger(
sys.stdout,
level=_lvl,
colorize=True,
)
# logger.add(
# _log_file,
# level=_lvl,
# format=format_log_record,
# rotation="00:00",
# retention="3 days",
# backtrace=True,
# diagnose=True,
# enqueue=True,
# )
__init_logger()