a
This commit is contained in:
parent
59b8e7b44f
commit
e092a36640
|
|
@ -16,6 +16,9 @@ router = APIRouter(prefix="/api/system")
|
||||||
|
|
||||||
LOG_GROUPS = ("error", "warning", "info")
|
LOG_GROUPS = ("error", "warning", "info")
|
||||||
|
|
||||||
|
# 避免 Redis 异常刷屏(前端可能自动刷新)
|
||||||
|
_last_logs_redis_err_ts: float = 0.0
|
||||||
|
|
||||||
|
|
||||||
def _logs_prefix() -> str:
|
def _logs_prefix() -> str:
|
||||||
return (os.getenv("REDIS_LOG_LIST_PREFIX", "ats:logs").strip() or "ats:logs")
|
return (os.getenv("REDIS_LOG_LIST_PREFIX", "ats:logs").strip() or "ats:logs")
|
||||||
|
|
@ -249,7 +252,15 @@ def _get_redis_client_for_logs():
|
||||||
client = redis.from_url(redis_url, **kwargs)
|
client = redis.from_url(redis_url, **kwargs)
|
||||||
client.ping()
|
client.ping()
|
||||||
return client
|
return client
|
||||||
except Exception:
|
except Exception as e:
|
||||||
|
# 把错误尽量打到 api.log(你现在看的文件)
|
||||||
|
global _last_logs_redis_err_ts
|
||||||
|
import time as _t
|
||||||
|
|
||||||
|
now = _t.time()
|
||||||
|
if now - _last_logs_redis_err_ts > 30:
|
||||||
|
_last_logs_redis_err_ts = now
|
||||||
|
logger.warning(f"日志模块 Redis 连接失败。REDIS_URL={os.getenv('REDIS_URL', '')} err={e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -370,6 +381,7 @@ async def logs_overview(x_admin_token: Optional[str] = Header(default=None, alia
|
||||||
if client is None:
|
if client is None:
|
||||||
raise HTTPException(status_code=503, detail="Redis 不可用,无法读取日志概览")
|
raise HTTPException(status_code=503, detail="Redis 不可用,无法读取日志概览")
|
||||||
|
|
||||||
|
try:
|
||||||
cfg = _read_logs_config(client)
|
cfg = _read_logs_config(client)
|
||||||
|
|
||||||
day = _beijing_yyyymmdd()
|
day = _beijing_yyyymmdd()
|
||||||
|
|
@ -404,7 +416,16 @@ async def logs_overview(x_admin_token: Optional[str] = Header(default=None, alia
|
||||||
"llen": llen,
|
"llen": llen,
|
||||||
"added_today": added_today,
|
"added_today": added_today,
|
||||||
},
|
},
|
||||||
|
"meta": {
|
||||||
|
"redis_url": os.getenv("REDIS_URL", ""),
|
||||||
|
"keys": {g: _logs_key_for_group(g) for g in LOG_GROUPS},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"logs_overview 失败: {e}", exc_info=True)
|
||||||
|
raise HTTPException(status_code=500, detail=f"logs_overview failed: {e}")
|
||||||
|
|
||||||
|
|
||||||
@router.put("/logs/config")
|
@router.put("/logs/config")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user