统一 LHFC 日志时间输出格式

使得 stream_name 与 mes_name 同步
pull/937/head
Bakadax 2025-05-17 08:29:52 +08:00
parent 2080c02f54
commit 09ec1502cd
2 changed files with 89 additions and 37 deletions

View File

@ -142,46 +142,98 @@ class ChatManager:
Returns: Returns:
ChatStream: 聊天流对象 ChatStream: 聊天流对象
""" """
# 生成stream_id stream_id = self._generate_stream_id(platform, user_info, group_info)
try:
stream_id = self._generate_stream_id(platform, user_info, group_info)
# 检查内存中是否存在 # mes_name: 当前调用此函数时,根据传入参数确定的最新聊天名称
if stream_id in self.streams: current_mes_name = None
stream = self.streams[stream_id] if group_info and group_info.group_name:
# 更新用户信息和群组信息 current_mes_name = group_info.group_name
stream.update_active_time() elif user_info and user_info.user_nickname: # private chat
stream = copy.deepcopy(stream) current_mes_name = f"{user_info.user_nickname}的私聊"
stream.user_info = user_info
if group_info:
stream.group_info = group_info
return stream
# 检查数据库中是否存在 # 检查内存中是否存在
data = db.chat_streams.find_one({"stream_id": stream_id}) if stream_id in self.streams:
if data: stream_instance = self.streams[stream_id] # 获取内存中的实际stream对象
stream = ChatStream.from_dict(data)
# 更新用户信息和群组信息 # stream_name: 从内存中获取的、可能存在的旧名称,用于比较
stream.user_info = user_info # 这个名称是 get_stream_name() 在此函数更新stream_instance前会返回的名称
old_effective_stream_name = None
if stream_instance.group_info and stream_instance.group_info.group_name:
old_effective_stream_name = stream_instance.group_info.group_name
elif stream_instance.user_info and stream_instance.user_info.user_nickname:
old_effective_stream_name = f"{stream_instance.user_info.user_nickname}的私聊"
# 判断名称是否发生变化
if current_mes_name is not None and old_effective_stream_name != current_mes_name:
logger.info(f"聊天流名称变更 (来自内存): ID '{stream_id}''{old_effective_stream_name}' 变为 '{current_mes_name}'. 更新数据库.")
# 更新内存中的stream_instance对象的 用户信息和群组信息(这将包含新的名称)
stream_instance.user_info = user_info
if group_info: if group_info:
stream.group_info = group_info stream_instance.group_info = group_info
stream.update_active_time() else: # 处理从群聊变私聊或群信息消失的情况
stream_instance.group_info = None
stream_instance.update_active_time() # 更新活跃时间也会将saved标记为False
await self._save_stream(stream_instance) # 保存更新到数据库
else: else:
# 创建新的聊天流 # 名称未变或无法确定当前新名称,仅更新活动时间和确保信息最新
stream = ChatStream( stream_instance.user_info = user_info
stream_id=stream_id, if group_info:
platform=platform, stream_instance.group_info = group_info
user_info=user_info, else:
group_info=group_info, stream_instance.group_info = None
) stream_instance.update_active_time()
except Exception as e:
logger.error(f"创建聊天流失败: {e}")
raise e
# 保存到内存和数据库 # self.streams[stream_id] 已经是更新后的 stream_instance
self.streams[stream_id] = stream return copy.deepcopy(self.streams[stream_id]) # 返回一个副本,避免外部直接修改缓存
await self._save_stream(stream)
return copy.deepcopy(stream) # 检查数据库中是否存在
data = db.chat_streams.find_one({"stream_id": stream_id})
if data:
stream_from_db = ChatStream.from_dict(data)
# stream_name: 从数据库加载的、可能存在的旧名称,用于比较
db_stream_name = None
if stream_from_db.group_info and stream_from_db.group_info.group_name:
db_stream_name = stream_from_db.group_info.group_name
elif stream_from_db.user_info and stream_from_db.user_info.user_nickname:
db_stream_name = f"{stream_from_db.user_info.user_nickname}的私聊"
# 判断名称是否发生变化
if current_mes_name is not None and db_stream_name != current_mes_name:
logger.info(f"聊天流名称变更 (来自数据库): ID '{stream_id}''{db_stream_name}' 变为 '{current_mes_name}'. 更新数据库.")
# 更新从数据库加载的stream对象的 用户信息和群组信息
stream_from_db.user_info = user_info
if group_info:
stream_from_db.group_info = group_info
else:
stream_from_db.group_info = None
# stream_from_db.update_active_time() # 将在下面统一处理
# stream_from_db.saved = False # update_active_time会处理
# 统一更新信息和活跃时间
stream_from_db.user_info = user_info
if group_info:
stream_from_db.group_info = group_info
else:
stream_from_db.group_info = None
stream_from_db.update_active_time()
self.streams[stream_id] = stream_from_db # 存入内存缓存
await self._save_stream(stream_from_db) # 保存到数据库(如果名称变化或活跃时间更新)
return copy.deepcopy(stream_from_db)
# 创建新的聊天流
# 此时,新流的名称自然就是 current_mes_name
new_stream = ChatStream(
stream_id=stream_id,
platform=platform,
user_info=user_info,
group_info=group_info,
)
self.streams[stream_id] = new_stream
await self._save_stream(new_stream)
logger.info(f"创建了新的聊天流: ID '{stream_id}', 名称 '{current_mes_name if current_mes_name else '未知'}'")
return copy.deepcopy(new_stream)
def get_stream(self, stream_id: str) -> Optional[ChatStream]: def get_stream(self, stream_id: str) -> Optional[ChatStream]:
"""通过stream_id获取聊天流""" """通过stream_id获取聊天流"""

View File

@ -210,7 +210,7 @@ class HeartFCProcessor:
# 7. 日志记录 # 7. 日志记录
mes_name = chat.group_info.group_name if chat.group_info else "私聊" mes_name = chat.group_info.group_name if chat.group_info else "私聊"
current_time = time.strftime("%H%M分%S秒", time.localtime(message.message_info.time)) current_time = time.strftime("%H:%M:%S", time.localtime(message.message_info.time))
logger.info( logger.info(
f"[{current_time}][{mes_name}]" f"[{current_time}][{mes_name}]"
f"{userinfo.user_nickname}:" f"{userinfo.user_nickname}:"