From 82624afe6840cfe2ec16fe99fee0423674436d92 Mon Sep 17 00:00:00 2001 From: TTS Service Date: Fri, 27 Mar 2026 14:26:17 +0800 Subject: [PATCH] feat: add logging for TTS calls with timing and error details --- app/main.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 59f8acb..37a58a0 100644 --- a/app/main.py +++ b/app/main.py @@ -9,6 +9,8 @@ import base64 import subprocess import uuid import asyncio +import logging +import time from contextlib import asynccontextmanager from pathlib import Path @@ -22,6 +24,15 @@ from sqlalchemy.orm import DeclarativeBase import config +# ── Logging ─────────────────────────────────────────────────────────────── + +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s [%(levelname)s] %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", +) +logger = logging.getLogger("tts-service") + # ── Database ────────────────────────────────────────────────────────────── engine = create_async_engine(config.DATABASE_URL, echo=False) @@ -76,20 +87,30 @@ async def call_mimo_tts(text: str, style: str = "") -> bytes: "api-key": config.MIMO_API_KEY, } + t0 = time.time() + logger.info(f"MiMo TTS 请求: text_len={len(text)}, style={style or '(默认)'}") + async with httpx.AsyncClient(timeout=120) as client: resp = await client.post(config.MIMO_API_ENDPOINT, json=payload, headers=headers) + elapsed = round(time.time() - t0, 2) + if resp.status_code != 200: + logger.error(f"MiMo TTS 错误: HTTP {resp.status_code}, 耗时 {elapsed}s, 响应: {resp.text[:200]}") raise HTTPException(502, f"MiMo TTS API 错误: HTTP {resp.status_code} - {resp.text[:300]}") data = resp.json() if data.get("error"): + logger.error(f"MiMo TTS 业务错误: {data['error']}, 耗时 {elapsed}s") raise HTTPException(502, f"MiMo TTS 错误: {data['error']}") try: audio_b64 = data["choices"][0]["message"]["audio"]["data"] - return base64.b64decode(audio_b64) + wav_bytes = base64.b64decode(audio_b64) + logger.info(f"MiMo TTS 成功: wav_size={len(wav_bytes)} bytes, 耗时 {elapsed}s") + return wav_bytes except (KeyError, IndexError, TypeError) as e: + logger.error(f"MiMo TTS 响应解析失败: {e}, 耗时 {elapsed}s") raise HTTPException(502, f"MiMo TTS 响应解析失败: {e}")