🦄 refactor(log): colorful log

This commit is contained in:
kalicyh
2025-02-18 00:07:19 +08:00
parent 921e3c2c75
commit 2acee60d73
32 changed files with 423 additions and 190 deletions
+9 -9
View File
@@ -2,7 +2,7 @@ import time
import wave
import os
from abc import ABC, abstractmethod
import logging
from config.logger import setup_logging
from typing import Optional, Tuple, List
import uuid
@@ -10,8 +10,8 @@ import opuslib
from funasr import AutoModel
from funasr.utils.postprocess_utils import rich_transcription_postprocess
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class ASR(ABC):
@abstractmethod
@@ -55,7 +55,7 @@ class FunASR(ASR):
pcm_frame = decoder.decode(opus_packet, 960) # 960 samples = 60ms
pcm_data.append(pcm_frame)
except opuslib.OpusError as e:
logger.error(f"Opus解码错误: {e}", exc_info=True)
logger.bind(tag=TAG).error(f"Opus解码错误: {e}", exc_info=True)
with wave.open(file_path, "wb") as wf:
wf.setnchannels(1)
@@ -72,7 +72,7 @@ class FunASR(ASR):
# 保存音频文件
start_time = time.time()
file_path = self.save_audio_to_file(opus_data, session_id)
logger.debug(f"音频文件保存耗时: {time.time() - start_time:.3f}s | 路径: {file_path}")
logger.bind(tag=TAG).debug(f"音频文件保存耗时: {time.time() - start_time:.3f}s | 路径: {file_path}")
# 语音识别
start_time = time.time()
@@ -84,12 +84,12 @@ class FunASR(ASR):
batch_size_s=60,
)
text = rich_transcription_postprocess(result[0]["text"])
logger.debug(f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}")
logger.bind(tag=TAG).debug(f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}")
return text, file_path
except Exception as e:
logger.error(f"语音识别失败: {e}", exc_info=True)
logger.bind(tag=TAG).error(f"语音识别失败: {e}", exc_info=True)
return None, None
finally:
@@ -97,9 +97,9 @@ class FunASR(ASR):
if self.delete_audio_file and file_path and os.path.exists(file_path):
try:
os.remove(file_path)
logger.debug(f"已删除临时音频文件: {file_path}")
logger.bind(tag=TAG).debug(f"已删除临时音频文件: {file_path}")
except Exception as e:
logger.error(f"文件删除失败: {file_path} | 错误: {e}")
logger.bind(tag=TAG).error(f"文件删除失败: {file_path} | 错误: {e}")
def create_instance(class_name: str, *args, **kwargs) -> ASR:
+2 -2
View File
@@ -1,13 +1,13 @@
import os
import sys
import logging
from config.logger import setup_logging
import importlib
from datetime import datetime
from core.utils.util import is_segment
from core.utils.util import get_string_no_punctuation_or_emoji
from core.utils.util import read_config, get_project_dir
logger = logging.getLogger(__name__)
logger = setup_logging()
def create_instance(class_name, *args, **kwargs):
+6 -5
View File
@@ -1,8 +1,9 @@
import asyncio
from typing import Dict
import logging
from config.logger import setup_logging
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class FileLockManager:
_instance = None
@@ -25,7 +26,7 @@ class FileLockManager:
"""获取锁"""
lock = cls.get_lock(file_path)
await lock.acquire()
logger.debug(f"Acquired lock for {file_path}")
logger.bind(tag=TAG).debug(f"Acquired lock for {file_path}")
@classmethod
def release_lock(cls, file_path: str):
@@ -33,6 +34,6 @@ class FileLockManager:
if file_path in cls._locks:
try:
cls._locks[file_path].release()
logger.debug(f"Released lock for {file_path}")
logger.bind(tag=TAG).debug(f"Released lock for {file_path}")
except RuntimeError as e:
logger.warning(f"Failed to release lock for {file_path}: {e}")
logger.bind(tag=TAG).warning(f"Failed to release lock for {file_path}: {e}")
+2 -2
View File
@@ -1,11 +1,11 @@
import os
import sys
import logging
from config.logger import setup_logging
import importlib
from datetime import datetime
from core.utils.util import read_config, get_project_dir
logger = logging.getLogger(__name__)
logger = setup_logging()
def create_instance(class_name, *args, **kwargs):
+6 -6
View File
@@ -1,12 +1,12 @@
from abc import ABC, abstractmethod
import logging
from config.logger import setup_logging
import opuslib
import time
import numpy as np
import torch
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class VAD(ABC):
@abstractmethod
@@ -17,7 +17,7 @@ class VAD(ABC):
class SileroVAD(VAD):
def __init__(self, config):
logger.info("SileroVAD", config)
logger.bind(tag=TAG).info("SileroVAD", config)
self.model, self.utils = torch.hub.load(repo_or_dir=config["model_dir"],
source='local',
model='silero_vad',
@@ -60,9 +60,9 @@ class SileroVAD(VAD):
return client_have_voice
except opuslib.OpusError as e:
logger.info(f"解码错误: {e}")
logger.bind(tag=TAG).info(f"解码错误: {e}")
except Exception as e:
logger.error(f"Error processing audio packet: {e}")
logger.bind(tag=TAG).error(f"Error processing audio packet: {e}")
def create_instance(class_name, *args, **kwargs) -> VAD: