diff --git a/src/common/logger.py b/src/common/logger.py index f69a95222..30a97e927 100644 --- a/src/common/logger.py +++ b/src/common/logger.py @@ -5,7 +5,58 @@ import os from types import ModuleType from pathlib import Path from dotenv import load_dotenv -# from ..plugins.chat.config import global_config +''' +日志颜色说明: + +1. 主程序(Main) +浅黄色标题 | 浅黄色消息 + +2. 海马体(Memory) +浅黄色标题 | 浅黄色消息 + +3. PFC(前额叶皮质) +浅绿色标题 | 浅绿色消息 + +4. 心情(Mood) +品红色标题 | 品红色消息 + +5. 工具使用(Tool) +品红色标题 | 品红色消息 + +6. 关系(Relation) +浅品红色标题 | 浅品红色消息 + +7. 配置(Config) +浅青色标题 | 浅青色消息 + +8. 麦麦大脑袋 +浅绿色标题 | 浅绿色消息 + +9. 在干嘛 +青色标题 | 青色消息 + +10. 麦麦组织语言 +浅绿色标题 | 浅绿色消息 + +11. 见闻(Chat) +浅蓝色标题 | 绿色消息 + +12. 表情包(Emoji) +橙色标题 | 橙色消息 fg #FFD700 + +13. 子心流 + +13. 其他模块 +模块名标题 | 对应颜色消息 + + +注意: +1. 级别颜色遵循loguru默认配置 +2. 可通过环境变量修改日志级别 +''' + + + # 加载 .env 文件 env_path = Path(__file__).resolve().parent.parent.parent / ".env" @@ -88,25 +139,6 @@ MAIN_STYLE_CONFIG = { }, } -# 海马体日志样式配置 -MEMORY_STYLE_CONFIG = { - "advanced": { - "console_format": ( - "{time:YYYY-MM-DD HH:mm:ss} | " - "{level: <8} | " - "海马体 | " - "{message}" - ), - "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 海马体 | {message}", - }, - "simple": { - "console_format": ( - "{time:MM-DD HH:mm} | 海马体 | {message}" - ), - "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 海马体 | {message}", - }, -} - # pfc配置 PFC_STYLE_CONFIG = { "advanced": { @@ -314,6 +346,24 @@ REMOTE_STYLE_CONFIG = { } SUB_HEARTFLOW_STYLE_CONFIG = { + "advanced": { + "console_format": ( + "{time:YYYY-MM-DD HH:mm:ss} | " + "{level: <8} | " + "麦麦水群 | " + "{message}" + ), + "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦小脑袋 | {message}", + }, + "simple": { + "console_format": ( + "{time:MM-DD HH:mm} | 麦麦水群 | {message}" + ), # noqa: E501 + "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦水群 | {message}", + }, +} + +SUB_HEARTFLOW_MIND_STYLE_CONFIG = { "advanced": { "console_format": ( "{time:YYYY-MM-DD HH:mm:ss} | " @@ -325,12 +375,30 @@ SUB_HEARTFLOW_STYLE_CONFIG = { }, "simple": { "console_format": ( - "{time:MM-DD HH:mm} | 麦麦小脑袋 | {message}" + "{time:MM-DD HH:mm} | 麦麦小脑袋 | {message}" ), # noqa: E501 "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦小脑袋 | {message}", }, } +SUBHEARTFLOW_MANAGER_STYLE_CONFIG = { + "advanced": { + "console_format": ( + "{time:YYYY-MM-DD HH:mm:ss} | " + "{level: <8} | " + "麦麦水群[管理] | " + "{message}" + ), + "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦水群[管理] | {message}", + }, + "simple": { + "console_format": ( + "{time:MM-DD HH:mm} | 麦麦水群[管理] | {message}" + ), # noqa: E501 + "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦水群[管理] | {message}", + }, +} + BASE_TOOL_STYLE_CONFIG = { "advanced": { "console_format": ( @@ -403,24 +471,6 @@ BACKGROUND_TASKS_STYLE_CONFIG = { }, } -SUBHEARTFLOW_MANAGER_STYLE_CONFIG = { - "advanced": { - "console_format": ( - "{time:YYYY-MM-DD HH:mm:ss} | " - "{level: <8} | " - "小脑袋管理 | " - "{message}" - ), - "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 小脑袋管理 | {message}", - }, - "simple": { - "console_format": ( - "{time:MM-DD HH:mm} | 小脑袋管理 | {message}" - ), # noqa: E501 - "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 小脑袋管理 | {message}", - }, -} - WILLING_STYLE_CONFIG = { "advanced": { "console_format": ( @@ -453,19 +503,20 @@ PFC_ACTION_PLANNER_STYLE_CONFIG = { }, } +# EMOJI,橙色,全着色 EMOJI_STYLE_CONFIG = { "advanced": { "console_format": ( "{time:YYYY-MM-DD HH:mm:ss} | " "{level: <8} | " - "表情 | " + "表情包 | " "{message}" ), - "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 表情 | {message}", + "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 表情包 | {message}", }, "simple": { - "console_format": "{time:MM-DD HH:mm} | 表情 | {message} ", # noqa: E501 - "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 表情 | {message}", + "console_format": "{time:MM-DD HH:mm} | 表情包 | {message} ", # noqa: E501 + "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 表情包 | {message}", }, } @@ -485,6 +536,27 @@ MAI_STATE_CONFIG = { }, } + +# 海马体日志样式配置 +MEMORY_STYLE_CONFIG = { + "advanced": { + "console_format": ( + "{time:YYYY-MM-DD HH:mm:ss} | " + "{level: <8} | " + "海马体 | " + "{message}" + ), + "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 海马体 | {message}", + }, + "simple": { + "console_format": ( + "{time:MM-DD HH:mm} | 海马体 | {message}" + ), + "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 海马体 | {message}", + }, +} + + # LPMM配置 LPMM_STYLE_CONFIG = { "advanced": { @@ -498,7 +570,7 @@ LPMM_STYLE_CONFIG = { }, "simple": { "console_format": ( - "{time:MM-DD HH:mm} | LPMM | {message}" + "{time:MM-DD HH:mm} | LPMM | {message}" ), "file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | LPMM | {message}", }, @@ -575,6 +647,9 @@ HEARTFLOW_STYLE_CONFIG = HEARTFLOW_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else SUB_HEARTFLOW_STYLE_CONFIG = ( SUB_HEARTFLOW_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else SUB_HEARTFLOW_STYLE_CONFIG["advanced"] ) # noqa: E501 +SUB_HEARTFLOW_MIND_STYLE_CONFIG = ( + SUB_HEARTFLOW_MIND_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else SUB_HEARTFLOW_MIND_STYLE_CONFIG["advanced"] +) WILLING_STYLE_CONFIG = WILLING_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else WILLING_STYLE_CONFIG["advanced"] MAI_STATE_CONFIG = MAI_STATE_CONFIG["simple"] if SIMPLE_OUTPUT else MAI_STATE_CONFIG["advanced"] CONFIG_STYLE_CONFIG = CONFIG_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else CONFIG_STYLE_CONFIG["advanced"] diff --git a/src/config/config.py b/src/config/config.py index 996b27385..1cc58f71b 100644 --- a/src/config/config.py +++ b/src/config/config.py @@ -28,7 +28,7 @@ logger = get_module_logger("config", config=config_config) # 考虑到,实际上配置文件中的mai_version是不会自动更新的,所以采用硬编码 is_test = True mai_version_main = "0.6.3" -mai_version_fix = "snapshot-4" +mai_version_fix = "snapshot-5" if mai_version_fix: if is_test: diff --git a/src/heart_flow/interest_logger.py b/src/heart_flow/interest_logger.py index 05a7da39d..62063f073 100644 --- a/src/heart_flow/interest_logger.py +++ b/src/heart_flow/interest_logger.py @@ -54,7 +54,7 @@ class InterestLogger: results = {} if not all_flows: - logger.debug("未找到任何子心流状态") + # logger.debug("未找到任何子心流状态") return results for subheartflow in all_flows: @@ -109,7 +109,7 @@ class InterestLogger: } if not all_subflow_states: - logger.debug("没有获取到任何子心流状态,仅记录主心流状态") + # logger.debug("没有获取到任何子心流状态,仅记录主心流状态") with open(self._history_log_file_path, "a", encoding="utf-8") as f: f.write(json.dumps(log_entry_base, ensure_ascii=False) + "\n") return diff --git a/src/heart_flow/mind.py b/src/heart_flow/mind.py index e806d18ae..a40ee6ef7 100644 --- a/src/heart_flow/mind.py +++ b/src/heart_flow/mind.py @@ -1,7 +1,7 @@ import traceback from typing import TYPE_CHECKING -from src.common.logger import get_module_logger +from src.common.logger import get_module_logger, LogConfig, SUB_HEARTFLOW_MIND_STYLE_CONFIG from src.plugins.models.utils_model import LLMRequest from src.individuality.individuality import Individuality from src.plugins.utils.prompt_builder import global_prompt_manager @@ -12,7 +12,12 @@ if TYPE_CHECKING: from src.heart_flow.subheartflow_manager import SubHeartflowManager from src.heart_flow.mai_state_manager import MaiStateInfo -logger = get_module_logger("mind") +mind_log_config = LogConfig( + console_format=SUB_HEARTFLOW_MIND_STYLE_CONFIG["console_format"], + file_format=SUB_HEARTFLOW_MIND_STYLE_CONFIG["file_format"], +) + +logger = get_module_logger("mind", config=mind_log_config) class Mind: diff --git a/src/heart_flow/sub_heartflow.py b/src/heart_flow/sub_heartflow.py index 7a6e009c7..7397a37f3 100644 --- a/src/heart_flow/sub_heartflow.py +++ b/src/heart_flow/sub_heartflow.py @@ -26,13 +26,6 @@ subheartflow_config = LogConfig( ) logger = get_module_logger("subheartflow", config=subheartflow_config) -interest_log_config = LogConfig( - console_format=SUB_HEARTFLOW_STYLE_CONFIG["console_format"], - file_format=SUB_HEARTFLOW_STYLE_CONFIG["file_format"], -) -interest_logger = get_module_logger("InterestChatting", config=interest_log_config) - - base_reply_probability = 0.05 probability_increase_rate_per_second = 0.08 max_reply_probability = 1 @@ -97,7 +90,7 @@ class InterestChatting: # 异常情况处理 if self.decay_rate_per_second <= 0: - interest_logger.warning(f"衰减率({self.decay_rate_per_second})无效,重置兴趣值为0") + logger.warning(f"衰减率({self.decay_rate_per_second})无效,重置兴趣值为0") self.interest_level = 0.0 return @@ -106,7 +99,7 @@ class InterestChatting: decay_factor = math.pow(self.decay_rate_per_second, self.update_interval) self.interest_level *= decay_factor except ValueError as e: - interest_logger.error( + logger.error( f"衰减计算错误: {e} 参数: 衰减率={self.decay_rate_per_second} 时间差={self.update_interval} 当前兴趣={self.interest_level}" ) self.interest_level = 0.0 @@ -161,46 +154,46 @@ class InterestChatting: # 正常超时,继续循环 continue except asyncio.CancelledError: - interest_logger.info("InterestChatting 更新循环被取消。") + logger.info("InterestChatting 更新循环被取消。") break except Exception as e: - interest_logger.error(f"InterestChatting 更新循环出错: {e}") - interest_logger.error(traceback.format_exc()) + logger.error(f"InterestChatting 更新循环出错: {e}") + logger.error(traceback.format_exc()) # 防止错误导致CPU飙升,稍作等待 await asyncio.sleep(5) - interest_logger.info("InterestChatting 更新循环已停止。") + logger.info("InterestChatting 更新循环已停止。") def start_updates(self, update_interval: float = 1.0): """启动后台更新任务""" if self.update_task is None or self.update_task.done(): self._stop_event.clear() self.update_task = asyncio.create_task(self._run_update_loop(update_interval)) - interest_logger.debug("后台兴趣更新任务已创建并启动。") + logger.debug("后台兴趣更新任务已创建并启动。") else: - interest_logger.debug("后台兴趣更新任务已在运行中。") + logger.debug("后台兴趣更新任务已在运行中。") async def stop_updates(self): """停止后台更新任务""" if self.update_task and not self.update_task.done(): - interest_logger.info("正在停止 InterestChatting 后台更新任务...") + logger.info("正在停止 InterestChatting 后台更新任务...") self._stop_event.set() # 发送停止信号 try: # 等待任务结束,设置超时 await asyncio.wait_for(self.update_task, timeout=5.0) - interest_logger.info("InterestChatting 后台更新任务已成功停止。") + logger.info("InterestChatting 后台更新任务已成功停止。") except asyncio.TimeoutError: - interest_logger.warning("停止 InterestChatting 后台任务超时,尝试取消...") + logger.warning("停止 InterestChatting 后台任务超时,尝试取消...") self.update_task.cancel() try: await self.update_task # 等待取消完成 except asyncio.CancelledError: - interest_logger.info("InterestChatting 后台更新任务已被取消。") + logger.info("InterestChatting 后台更新任务已被取消。") except Exception as e: - interest_logger.error(f"停止 InterestChatting 后台任务时发生异常: {e}") + logger.error(f"停止 InterestChatting 后台任务时发生异常: {e}") finally: self.update_task = None else: - interest_logger.debug("InterestChatting 后台更新任务未运行或已完成。") + logger.debug("InterestChatting 后台更新任务未运行或已完成。") # --- 结束 新增方法 ---