a
This commit is contained in:
parent
59b8e7b44f
commit
e092a36640
|
|
@ -16,6 +16,9 @@ router = APIRouter(prefix="/api/system")
|
|||
|
||||
LOG_GROUPS = ("error", "warning", "info")
|
||||
|
||||
# 避免 Redis 异常刷屏(前端可能自动刷新)
|
||||
_last_logs_redis_err_ts: float = 0.0
|
||||
|
||||
|
||||
def _logs_prefix() -> str:
|
||||
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.ping()
|
||||
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
|
||||
|
||||
|
||||
|
|
@ -370,41 +381,51 @@ async def logs_overview(x_admin_token: Optional[str] = Header(default=None, alia
|
|||
if client is None:
|
||||
raise HTTPException(status_code=503, detail="Redis 不可用,无法读取日志概览")
|
||||
|
||||
cfg = _read_logs_config(client)
|
||||
try:
|
||||
cfg = _read_logs_config(client)
|
||||
|
||||
day = _beijing_yyyymmdd()
|
||||
stats_prefix = _logs_stats_prefix()
|
||||
day = _beijing_yyyymmdd()
|
||||
stats_prefix = _logs_stats_prefix()
|
||||
|
||||
pipe = client.pipeline()
|
||||
for g in LOG_GROUPS:
|
||||
pipe.llen(_logs_key_for_group(g))
|
||||
for g in LOG_GROUPS:
|
||||
pipe.get(f"{stats_prefix}:{day}:{g}")
|
||||
res = pipe.execute()
|
||||
pipe = client.pipeline()
|
||||
for g in LOG_GROUPS:
|
||||
pipe.llen(_logs_key_for_group(g))
|
||||
for g in LOG_GROUPS:
|
||||
pipe.get(f"{stats_prefix}:{day}:{g}")
|
||||
res = pipe.execute()
|
||||
|
||||
llen_vals = res[: len(LOG_GROUPS)]
|
||||
added_vals = res[len(LOG_GROUPS) :]
|
||||
llen_vals = res[: len(LOG_GROUPS)]
|
||||
added_vals = res[len(LOG_GROUPS) :]
|
||||
|
||||
llen: Dict[str, int] = {}
|
||||
added_today: Dict[str, int] = {}
|
||||
for i, g in enumerate(LOG_GROUPS):
|
||||
try:
|
||||
llen[g] = int(llen_vals[i] or 0)
|
||||
except Exception:
|
||||
llen[g] = 0
|
||||
try:
|
||||
added_today[g] = int(added_vals[i] or 0)
|
||||
except Exception:
|
||||
added_today[g] = 0
|
||||
llen: Dict[str, int] = {}
|
||||
added_today: Dict[str, int] = {}
|
||||
for i, g in enumerate(LOG_GROUPS):
|
||||
try:
|
||||
llen[g] = int(llen_vals[i] or 0)
|
||||
except Exception:
|
||||
llen[g] = 0
|
||||
try:
|
||||
added_today[g] = int(added_vals[i] or 0)
|
||||
except Exception:
|
||||
added_today[g] = 0
|
||||
|
||||
return {
|
||||
"config": cfg,
|
||||
"stats": {
|
||||
"day": day,
|
||||
"llen": llen,
|
||||
"added_today": added_today,
|
||||
},
|
||||
}
|
||||
return {
|
||||
"config": cfg,
|
||||
"stats": {
|
||||
"day": day,
|
||||
"llen": llen,
|
||||
"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")
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user