Files
wiki_crawler/backend/core/logger.py

24 lines
790 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# backend/core/logger.py
import logging
import sys
def setup_logging(level=logging.INFO):
"""
全局日志配置
关键点:强制将日志输出到 sys.stderr防止污染 sys.stdout 导致 MCP 协议崩溃。
"""
# 定义日志格式:时间 - 模块名 - 级别 - 内容
log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
# 配置根记录器
logging.basicConfig(
level=level,
format=log_format,
handlers=[
# 【绝对关键】使用 StreamHandler(sys.stderr)
# 这样日志会走标准错误通道,不会干扰 MCP 的标准输出通信
logging.StreamHandler(sys.stderr)
],
# 强制重新配置,防止被第三方库覆盖
force=True
)