23 lines
728 B
Python
23 lines
728 B
Python
"""错误日志持久化"""
|
|
|
|
import traceback
|
|
from datetime import datetime
|
|
from app.db.database import get_db
|
|
from app.db import tables
|
|
|
|
|
|
async def log_error(source: str, message: str, detail: str = "", level: str = "error"):
|
|
"""将错误写入数据库,失败时静默(不影响主流程)"""
|
|
try:
|
|
async with get_db() as db:
|
|
stmt = tables.error_logs_table.insert().values(
|
|
source=source,
|
|
level=level,
|
|
message=message,
|
|
detail=detail,
|
|
created_at=datetime.now(),
|
|
)
|
|
await db.execute(stmt)
|
|
await db.commit()
|
|
except Exception:
|
|
pass # 写日志失败不应影响主业务 |