feat(core): 添加会话日志记录功能

- 在 connection.py 和 receiveAudioHandle.py 中添加代码,将模型回复和用户输入记录到日志文件
- 日志文件保存在 data 目录下,文件名格式为 <device_id>-log.log
- 优化了日志内容格式,统一使用 deviceId、datetime、模型回复或用户说等字段
This commit is contained in:
zxb 2025-04-10 15:06:41 +08:00
parent ea39c692de
commit 9478d1a582
3 changed files with 18 additions and 4 deletions

View file

@ -28,6 +28,8 @@ from core.auth import AuthMiddleware, AuthenticationError
from core.utils.auth_code_gen import AuthCodeGenerator
from core.mcp.manager import MCPManager
from datetime import datetime
from core.utils.util import get_project_dir
TAG = __name__
auto_import_modules("plugins_func.functions")
@ -40,6 +42,7 @@ class ConnectionHandler:
def __init__(
self, config: Dict[str, Any], _vad, _asr, _llm, _tts, _memory, _intent
):
self.device_id = "nobody"
self.config = config
self.logger = setup_logging()
self.auth = AuthMiddleware(config)
@ -369,9 +372,7 @@ class ConnectionHandler:
# 处理最后剩余的文本
full_text = "".join(response_message)
self.logger.bind(tag=TAG).info(
f"deviceId: {self.conn.private_config.device_id},datetime: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")},模型回复: {full_text}"
)
remaining_text = full_text[processed_chars:]
if remaining_text:
segment_text = get_string_no_punctuation_or_emoji(remaining_text)
@ -751,6 +752,11 @@ class ConnectionHandler:
)
def speak_and_play(self, text, text_index=0):
log_content = f'deviceId: {self.device_id}, datetime: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}, 模型回复: {text}, text_index: {text_index}'
self.logger.bind(tag=TAG).info(log_content)
log_path = get_project_dir() + 'data/'+self.device_id+'-log.log'
with open(log_path, 'a', encoding='utf-8') as f:
f.write(log_content + '\n')
if text is None or len(text) <= 0:
self.logger.bind(tag=TAG).info(f"无需tts转换,query为空,{text}")
return None, text, text_index

View file

@ -4,6 +4,8 @@ from core.utils.util import remove_punctuation_and_length
from core.handle.sendAudioHandle import send_stt_message
from core.handle.intentHandler import handle_user_intent
from datetime import datetime
from core.utils.util import get_project_dir
TAG = __name__
logger = setup_logging()
@ -38,7 +40,12 @@ async def handleAudioMessage(conn, audio):
text, file_path = await conn.asr.speech_to_text(
conn.asr_audio, conn.session_id
)
logger.bind(tag=TAG).info(f"deviceId: {conn.private_config.device_id},datetime: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")},用户说: {text}")
log_content = f'deviceId: {conn.device_id},datetime: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")},用户说: {text}'
logger.bind(tag=TAG).info(log_content)
log_path = get_project_dir() + 'data/'+conn.device_id+'-log.log'
with open(log_path, 'a', encoding='utf-8') as f:
f.write(log_content + '\n')
text_len, _ = remove_punctuation_and_length(text)
if text_len > 0:
await startToChat(conn, text)

View file

@ -21,6 +21,7 @@ async def handleTextMessage(conn, message):
await conn.websocket.send(message)
return
if msg_json["type"] == "hello":
conn.device_id = msg_json["device_id"]
await handleHelloMessage(conn)
elif msg_json["type"] == "abort":
await handleAbortMessage(conn)