better 优化logger输出,清洁cmd
This commit is contained in:
2
bot.py
2
bot.py
@@ -14,8 +14,6 @@ from nonebot.adapters.onebot.v11 import Adapter
|
|||||||
import platform
|
import platform
|
||||||
from src.common.logger import get_module_logger
|
from src.common.logger import get_module_logger
|
||||||
|
|
||||||
|
|
||||||
# 配置主程序日志格式
|
|
||||||
logger = get_module_logger("main_bot")
|
logger = get_module_logger("main_bot")
|
||||||
|
|
||||||
# 获取没有加载env时的环境变量
|
# 获取没有加载env时的环境变量
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ from pathlib import Path
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
# from ..plugins.chat.config import global_config
|
# from ..plugins.chat.config import global_config
|
||||||
|
|
||||||
load_dotenv()
|
# 加载 .env.prod 文件
|
||||||
|
env_path = Path(__file__).resolve().parent.parent.parent / '.env.prod'
|
||||||
|
load_dotenv(dotenv_path=env_path)
|
||||||
|
|
||||||
# 保存原生处理器ID
|
# 保存原生处理器ID
|
||||||
default_handler_id = None
|
default_handler_id = None
|
||||||
@@ -29,8 +31,6 @@ _handler_registry: Dict[str, List[int]] = {}
|
|||||||
current_file_path = Path(__file__).resolve()
|
current_file_path = Path(__file__).resolve()
|
||||||
LOG_ROOT = "logs"
|
LOG_ROOT = "logs"
|
||||||
|
|
||||||
# 从环境变量获取是否启用高级输出
|
|
||||||
# ENABLE_ADVANCE_OUTPUT = True
|
|
||||||
ENABLE_ADVANCE_OUTPUT = False
|
ENABLE_ADVANCE_OUTPUT = False
|
||||||
|
|
||||||
if ENABLE_ADVANCE_OUTPUT:
|
if ENABLE_ADVANCE_OUTPUT:
|
||||||
@@ -82,8 +82,6 @@ else:
|
|||||||
"compression": "zip",
|
"compression": "zip",
|
||||||
}
|
}
|
||||||
|
|
||||||
# 控制nonebot日志输出的环境变量
|
|
||||||
NONEBOT_LOG_ENABLED = False
|
|
||||||
|
|
||||||
# 海马体日志样式配置
|
# 海马体日志样式配置
|
||||||
MEMORY_STYLE_CONFIG = {
|
MEMORY_STYLE_CONFIG = {
|
||||||
@@ -187,7 +185,6 @@ LLM_STYLE_CONFIG = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Topic日志样式配置
|
# Topic日志样式配置
|
||||||
TOPIC_STYLE_CONFIG = {
|
TOPIC_STYLE_CONFIG = {
|
||||||
"advanced": {
|
"advanced": {
|
||||||
@@ -222,15 +219,47 @@ TOPIC_STYLE_CONFIG = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Topic日志样式配置
|
||||||
|
CHAT_STYLE_CONFIG = {
|
||||||
|
"advanced": {
|
||||||
|
"console_format": (
|
||||||
|
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
|
||||||
|
"<level>{level: <8}</level> | "
|
||||||
|
"<cyan>{extra[module]: <12}</cyan> | "
|
||||||
|
"<light-blue>见闻</light-blue> | "
|
||||||
|
"<level>{message}</level>"
|
||||||
|
),
|
||||||
|
"file_format": (
|
||||||
|
"{time:YYYY-MM-DD HH:mm:ss} | "
|
||||||
|
"{level: <8} | "
|
||||||
|
"{extra[module]: <15} | "
|
||||||
|
"见闻 | "
|
||||||
|
"{message}"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
"simple": {
|
||||||
|
"console_format": (
|
||||||
|
"<green>{time:MM-DD HH:mm}</green> | "
|
||||||
|
"<light-blue>见闻</light-blue> | "
|
||||||
|
"{message}"
|
||||||
|
),
|
||||||
|
"file_format": (
|
||||||
|
"{time:YYYY-MM-DD HH:mm:ss} | "
|
||||||
|
"{level: <8} | "
|
||||||
|
"{extra[module]: <15} | "
|
||||||
|
"见闻 | "
|
||||||
|
"{message}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# 根据ENABLE_ADVANCE_OUTPUT选择配置
|
# 根据ENABLE_ADVANCE_OUTPUT选择配置
|
||||||
MEMORY_STYLE_CONFIG = MEMORY_STYLE_CONFIG["advanced"] if ENABLE_ADVANCE_OUTPUT else MEMORY_STYLE_CONFIG["simple"]
|
MEMORY_STYLE_CONFIG = MEMORY_STYLE_CONFIG["advanced"] if ENABLE_ADVANCE_OUTPUT else MEMORY_STYLE_CONFIG["simple"]
|
||||||
TOPIC_STYLE_CONFIG = TOPIC_STYLE_CONFIG["advanced"] if ENABLE_ADVANCE_OUTPUT else TOPIC_STYLE_CONFIG["simple"]
|
TOPIC_STYLE_CONFIG = TOPIC_STYLE_CONFIG["advanced"] if ENABLE_ADVANCE_OUTPUT else TOPIC_STYLE_CONFIG["simple"]
|
||||||
SENDER_STYLE_CONFIG = SENDER_STYLE_CONFIG["advanced"] if ENABLE_ADVANCE_OUTPUT else SENDER_STYLE_CONFIG["simple"]
|
SENDER_STYLE_CONFIG = SENDER_STYLE_CONFIG["advanced"] if ENABLE_ADVANCE_OUTPUT else SENDER_STYLE_CONFIG["simple"]
|
||||||
LLM_STYLE_CONFIG = LLM_STYLE_CONFIG["advanced"] if ENABLE_ADVANCE_OUTPUT else LLM_STYLE_CONFIG["simple"]
|
LLM_STYLE_CONFIG = LLM_STYLE_CONFIG["advanced"] if ENABLE_ADVANCE_OUTPUT else LLM_STYLE_CONFIG["simple"]
|
||||||
|
CHAT_STYLE_CONFIG = CHAT_STYLE_CONFIG["advanced"] if ENABLE_ADVANCE_OUTPUT else CHAT_STYLE_CONFIG["simple"]
|
||||||
|
|
||||||
def filter_nonebot(record: dict) -> bool:
|
|
||||||
"""过滤nonebot的日志"""
|
|
||||||
return record["extra"].get("module") != "nonebot"
|
|
||||||
|
|
||||||
def is_registered_module(record: dict) -> bool:
|
def is_registered_module(record: dict) -> bool:
|
||||||
"""检查是否为已注册的模块"""
|
"""检查是否为已注册的模块"""
|
||||||
@@ -335,6 +364,7 @@ def remove_module_logger(module_name: str) -> None:
|
|||||||
|
|
||||||
|
|
||||||
# 添加全局默认处理器(只处理未注册模块的日志--->控制台)
|
# 添加全局默认处理器(只处理未注册模块的日志--->控制台)
|
||||||
|
# print(os.getenv("DEFAULT_CONSOLE_LOG_LEVEL", "SUCCESS"))
|
||||||
DEFAULT_GLOBAL_HANDLER = logger.add(
|
DEFAULT_GLOBAL_HANDLER = logger.add(
|
||||||
sink=sys.stderr,
|
sink=sys.stderr,
|
||||||
level=os.getenv("DEFAULT_CONSOLE_LOG_LEVEL", "SUCCESS"),
|
level=os.getenv("DEFAULT_CONSOLE_LOG_LEVEL", "SUCCESS"),
|
||||||
@@ -344,7 +374,7 @@ DEFAULT_GLOBAL_HANDLER = logger.add(
|
|||||||
"<cyan>{name: <12}</cyan> | "
|
"<cyan>{name: <12}</cyan> | "
|
||||||
"<level>{message}</level>"
|
"<level>{message}</level>"
|
||||||
),
|
),
|
||||||
filter=lambda record: is_unregistered_module(record) and filter_nonebot(record), # 只处理未注册模块的日志,并过滤nonebot
|
filter=lambda record: is_unregistered_module(record), # 只处理未注册模块的日志,并过滤nonebot
|
||||||
enqueue=True,
|
enqueue=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -367,6 +397,6 @@ DEFAULT_FILE_HANDLER = logger.add(
|
|||||||
retention=DEFAULT_CONFIG["retention"],
|
retention=DEFAULT_CONFIG["retention"],
|
||||||
compression=DEFAULT_CONFIG["compression"],
|
compression=DEFAULT_CONFIG["compression"],
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
filter=lambda record: is_unregistered_module(record) and filter_nonebot(record), # 只处理未注册模块的日志,并过滤nonebot
|
filter=lambda record: is_unregistered_module(record), # 只处理未注册模块的日志,并过滤nonebot
|
||||||
enqueue=True,
|
enqueue=True,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ from nonebot.adapters.onebot.v11 import (
|
|||||||
FriendRecallNoticeEvent,
|
FriendRecallNoticeEvent,
|
||||||
)
|
)
|
||||||
|
|
||||||
from src.common.logger import get_module_logger
|
|
||||||
from ..memory_system.memory import hippocampus
|
from ..memory_system.memory import hippocampus
|
||||||
from ..moods.moods import MoodManager # 导入情绪管理器
|
from ..moods.moods import MoodManager # 导入情绪管理器
|
||||||
from .config import global_config
|
from .config import global_config
|
||||||
@@ -33,7 +32,16 @@ from .utils_user import get_user_nickname, get_user_cardname, get_groupname
|
|||||||
from ..willing.willing_manager import willing_manager # 导入意愿管理器
|
from ..willing.willing_manager import willing_manager # 导入意愿管理器
|
||||||
from .message_base import UserInfo, GroupInfo, Seg
|
from .message_base import UserInfo, GroupInfo, Seg
|
||||||
|
|
||||||
logger = get_module_logger("chat_bot")
|
from src.common.logger import get_module_logger, CHAT_STYLE_CONFIG, LogConfig
|
||||||
|
# 定义日志配置
|
||||||
|
chat_config = LogConfig(
|
||||||
|
# 使用消息发送专用样式
|
||||||
|
console_format=CHAT_STYLE_CONFIG["console_format"],
|
||||||
|
file_format=CHAT_STYLE_CONFIG["file_format"]
|
||||||
|
)
|
||||||
|
|
||||||
|
# 配置主程序日志格式
|
||||||
|
logger = get_module_logger("chat_bot", config=chat_config)
|
||||||
|
|
||||||
|
|
||||||
class ChatBot:
|
class ChatBot:
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ class Message_Sender:
|
|||||||
message=message_send.raw_message,
|
message=message_send.raw_message,
|
||||||
auto_escape=False,
|
auto_escape=False,
|
||||||
)
|
)
|
||||||
logger.success(f"[调试] 发送消息“{message_preview}”成功")
|
logger.success(f"发送消息“{message_preview}”成功")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"[调试] 发生错误 {e}")
|
logger.error(f"[调试] 发生错误 {e}")
|
||||||
logger.error(f"[调试] 发送消息“{message_preview}”失败")
|
logger.error(f"[调试] 发送消息“{message_preview}”失败")
|
||||||
@@ -81,7 +81,7 @@ class Message_Sender:
|
|||||||
message=message_send.raw_message,
|
message=message_send.raw_message,
|
||||||
auto_escape=False,
|
auto_escape=False,
|
||||||
)
|
)
|
||||||
logger.success(f"[调试] 发送消息“{message_preview}”成功")
|
logger.success(f"发送消息“{message_preview}”成功")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"[调试] 发生错误 {e}")
|
logger.error(f"[调试] 发生错误 {e}")
|
||||||
logger.error(f"[调试] 发送消息“{message_preview}”失败")
|
logger.error(f"[调试] 发送消息“{message_preview}”失败")
|
||||||
|
|||||||
@@ -826,7 +826,7 @@ class Hippocampus:
|
|||||||
|
|
||||||
async def memory_activate_value(self, text: str, max_topics: int = 5, similarity_threshold: float = 0.3) -> int:
|
async def memory_activate_value(self, text: str, max_topics: int = 5, similarity_threshold: float = 0.3) -> int:
|
||||||
"""计算输入文本对记忆的激活程度"""
|
"""计算输入文本对记忆的激活程度"""
|
||||||
logger.info(f"[激活] 识别主题: {await self._identify_topics(text)}")
|
logger.info(f"识别主题: {await self._identify_topics(text)}")
|
||||||
|
|
||||||
# 识别主题
|
# 识别主题
|
||||||
identified_topics = await self._identify_topics(text)
|
identified_topics = await self._identify_topics(text)
|
||||||
@@ -858,7 +858,7 @@ class Hippocampus:
|
|||||||
|
|
||||||
activation = int(score * 50 * penalty)
|
activation = int(score * 50 * penalty)
|
||||||
logger.info(
|
logger.info(
|
||||||
f"[激活] 单主题「{topic}」- 相似度: {score:.3f}, 内容数: {content_count}, 激活值: {activation}")
|
f"单主题「{topic}」- 相似度: {score:.3f}, 内容数: {content_count}, 激活值: {activation}")
|
||||||
return activation
|
return activation
|
||||||
|
|
||||||
# 计算关键词匹配率,同时考虑内容数量
|
# 计算关键词匹配率,同时考虑内容数量
|
||||||
@@ -895,7 +895,7 @@ class Hippocampus:
|
|||||||
# 计算最终激活值
|
# 计算最终激活值
|
||||||
activation = int((topic_match + average_similarities) / 2 * 100)
|
activation = int((topic_match + average_similarities) / 2 * 100)
|
||||||
logger.info(
|
logger.info(
|
||||||
f"[激活] 匹配率: {topic_match:.3f}, 平均相似度: {average_similarities:.3f}, 激活值: {activation}")
|
f"匹配率: {topic_match:.3f}, 平均相似度: {average_similarities:.3f}, 激活值: {activation}")
|
||||||
|
|
||||||
return activation
|
return activation
|
||||||
|
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ class LLM_request:
|
|||||||
"timestamp": datetime.now(),
|
"timestamp": datetime.now(),
|
||||||
}
|
}
|
||||||
db.llm_usage.insert_one(usage_data)
|
db.llm_usage.insert_one(usage_data)
|
||||||
logger.info(
|
logger.debug(
|
||||||
f"Token使用情况 - 模型: {self.model_name}, "
|
f"Token使用情况 - 模型: {self.model_name}, "
|
||||||
f"用户: {user_id}, 类型: {request_type}, "
|
f"用户: {user_id}, 类型: {request_type}, "
|
||||||
f"提示词: {prompt_tokens}, 完成: {completion_tokens}, "
|
f"提示词: {prompt_tokens}, 完成: {completion_tokens}, "
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ class ChineseTypoGenerator:
|
|||||||
|
|
||||||
# 加载数据
|
# 加载数据
|
||||||
# print("正在加载汉字数据库,请稍候...")
|
# print("正在加载汉字数据库,请稍候...")
|
||||||
logger.info("正在加载汉字数据库,请稍候...")
|
# logger.info("正在加载汉字数据库,请稍候...")
|
||||||
|
|
||||||
self.pinyin_dict = self._create_pinyin_dict()
|
self.pinyin_dict = self._create_pinyin_dict()
|
||||||
self.char_frequency = self._load_or_create_char_frequency()
|
self.char_frequency = self._load_or_create_char_frequency()
|
||||||
|
|||||||
Reference in New Issue
Block a user