Add hub system logs page with PM2 stdout/stderr tabs for all exchanges.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,89 @@
|
|||||||
|
"""中控:读取 PM2 进程 stdout/stderr 日志(系统日志页)."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
LOG_TARGETS: dict[str, dict[str, str]] = {
|
||||||
|
"binance": {"label": "币安", "pm2_name": "crypto_binance"},
|
||||||
|
"gate": {"label": "Gate", "pm2_name": "crypto_gate"},
|
||||||
|
"okx": {"label": "OKX", "pm2_name": "crypto_okx"},
|
||||||
|
"hub": {"label": "中控", "pm2_name": "manual-trading-hub"},
|
||||||
|
}
|
||||||
|
|
||||||
|
DEFAULT_LINES = 200
|
||||||
|
MAX_LINES = 500
|
||||||
|
DEFAULT_TAIL_BYTES = 400_000
|
||||||
|
|
||||||
|
|
||||||
|
def pm2_logs_dir() -> Path:
|
||||||
|
raw = (os.getenv("PM2_HOME") or "").strip()
|
||||||
|
base = Path(raw) if raw else Path.home() / ".pm2"
|
||||||
|
return base / "logs"
|
||||||
|
|
||||||
|
|
||||||
|
def log_file_paths(pm2_name: str) -> tuple[Path, Path]:
|
||||||
|
logs_dir = pm2_logs_dir()
|
||||||
|
return logs_dir / f"{pm2_name}-out.log", logs_dir / f"{pm2_name}-error.log"
|
||||||
|
|
||||||
|
|
||||||
|
def tail_lines(
|
||||||
|
path: Path,
|
||||||
|
lines: int = DEFAULT_LINES,
|
||||||
|
*,
|
||||||
|
max_bytes: int = DEFAULT_TAIL_BYTES,
|
||||||
|
) -> str:
|
||||||
|
if not path.is_file():
|
||||||
|
return ""
|
||||||
|
try:
|
||||||
|
size = path.stat().st_size
|
||||||
|
with path.open("rb") as handle:
|
||||||
|
if size <= max_bytes:
|
||||||
|
data = handle.read()
|
||||||
|
else:
|
||||||
|
handle.seek(max(0, size - max_bytes))
|
||||||
|
data = handle.read()
|
||||||
|
text = data.decode("utf-8", errors="replace")
|
||||||
|
parts = text.splitlines()
|
||||||
|
if len(parts) > lines:
|
||||||
|
parts = parts[-lines:]
|
||||||
|
return "\n".join(parts)
|
||||||
|
except OSError:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def system_logs_meta() -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"ok": True,
|
||||||
|
"targets": [
|
||||||
|
{"key": key, "label": cfg["label"], "pm2_name": cfg["pm2_name"]}
|
||||||
|
for key, cfg in LOG_TARGETS.items()
|
||||||
|
],
|
||||||
|
"default_lines": DEFAULT_LINES,
|
||||||
|
"max_lines": MAX_LINES,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def load_system_logs(target: str, lines: int = DEFAULT_LINES) -> dict[str, Any]:
|
||||||
|
key = (target or "").strip().lower()
|
||||||
|
if key not in LOG_TARGETS:
|
||||||
|
raise KeyError(key)
|
||||||
|
cfg = LOG_TARGETS[key]
|
||||||
|
line_count = max(20, min(MAX_LINES, int(lines or DEFAULT_LINES)))
|
||||||
|
out_path, err_path = log_file_paths(cfg["pm2_name"])
|
||||||
|
return {
|
||||||
|
"ok": True,
|
||||||
|
"key": key,
|
||||||
|
"label": cfg["label"],
|
||||||
|
"pm2_name": cfg["pm2_name"],
|
||||||
|
"lines": line_count,
|
||||||
|
"out": tail_lines(out_path, line_count),
|
||||||
|
"err": tail_lines(err_path, line_count),
|
||||||
|
"out_exists": out_path.is_file(),
|
||||||
|
"err_exists": err_path.is_file(),
|
||||||
|
"out_path": str(out_path),
|
||||||
|
"err_path": str(err_path),
|
||||||
|
"updated_at": int(time.time()),
|
||||||
|
}
|
||||||
@@ -95,6 +95,7 @@ from lib.hub.hub_strategy_lib import (
|
|||||||
load_strategy_payload,
|
load_strategy_payload,
|
||||||
strategy_meta_payload,
|
strategy_meta_payload,
|
||||||
)
|
)
|
||||||
|
from lib.hub.hub_system_logs_lib import load_system_logs, system_logs_meta
|
||||||
from lib.hub.hub_macro_calendar_lib import (
|
from lib.hub.hub_macro_calendar_lib import (
|
||||||
MACRO_EVENT_LABELS,
|
MACRO_EVENT_LABELS,
|
||||||
MACRO_EVENT_TYPES,
|
MACRO_EVENT_TYPES,
|
||||||
@@ -971,6 +972,7 @@ def root_redirect():
|
|||||||
@app.get("/funds")
|
@app.get("/funds")
|
||||||
@app.get("/ai")
|
@app.get("/ai")
|
||||||
@app.get("/strategy")
|
@app.get("/strategy")
|
||||||
|
@app.get("/logs")
|
||||||
@app.get("/settings")
|
@app.get("/settings")
|
||||||
def shell_pages():
|
def shell_pages():
|
||||||
return _shell_page()
|
return _shell_page()
|
||||||
@@ -1081,6 +1083,7 @@ class SettingsDisplayBody(BaseModel):
|
|||||||
show_nav_ai: bool = True
|
show_nav_ai: bool = True
|
||||||
show_nav_calculator: bool = True
|
show_nav_calculator: bool = True
|
||||||
show_nav_strategy: bool = True
|
show_nav_strategy: bool = True
|
||||||
|
show_nav_logs: bool = True
|
||||||
|
|
||||||
|
|
||||||
class SupervisorSettingsBody(BaseModel):
|
class SupervisorSettingsBody(BaseModel):
|
||||||
@@ -3261,6 +3264,19 @@ def api_strategy_meta():
|
|||||||
return strategy_meta_payload()
|
return strategy_meta_payload()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/system-logs/meta")
|
||||||
|
def api_system_logs_meta():
|
||||||
|
return system_logs_meta()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/system-logs/{target}")
|
||||||
|
def api_system_logs(target: str, lines: int = 200):
|
||||||
|
try:
|
||||||
|
return load_system_logs(target, lines=lines)
|
||||||
|
except KeyError:
|
||||||
|
return JSONResponse({"ok": False, "msg": "unknown log target"}, status_code=404)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/strategy/{exchange_key}")
|
@app.get("/api/strategy/{exchange_key}")
|
||||||
def api_strategy_detail(exchange_key: str):
|
def api_strategy_detail(exchange_key: str):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ DEFAULT_DISPLAY = {
|
|||||||
"show_nav_ai": True,
|
"show_nav_ai": True,
|
||||||
"show_nav_calculator": True,
|
"show_nav_calculator": True,
|
||||||
"show_nav_strategy": True,
|
"show_nav_strategy": True,
|
||||||
|
"show_nav_logs": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
DEFAULT_EXCHANGES = [
|
DEFAULT_EXCHANGES = [
|
||||||
|
|||||||
@@ -8321,3 +8321,101 @@ html[data-theme="light"] .hub-options-summary strong {
|
|||||||
color: #142232;
|
color: #142232;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ── 系统日志页 ── */
|
||||||
|
.hub-logs-page-head {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 12px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
.hub-logs-page-actions {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
.hub-logs-toolbar {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 12px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
margin-bottom: 12px;
|
||||||
|
}
|
||||||
|
.hub-logs-tabs {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
.hub-logs-tab {
|
||||||
|
padding: 6px 14px;
|
||||||
|
border-radius: 999px;
|
||||||
|
border: 1px solid var(--border-soft);
|
||||||
|
background: var(--surface-2);
|
||||||
|
color: var(--text-soft);
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 0.88rem;
|
||||||
|
}
|
||||||
|
.hub-logs-tab.is-active {
|
||||||
|
background: var(--accent-soft);
|
||||||
|
border-color: var(--accent);
|
||||||
|
color: var(--text);
|
||||||
|
}
|
||||||
|
#hub-logs-status.is-err {
|
||||||
|
color: var(--danger, #f87171);
|
||||||
|
}
|
||||||
|
.hub-logs-layout {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||||
|
gap: 16px;
|
||||||
|
align-items: stretch;
|
||||||
|
}
|
||||||
|
.hub-logs-card {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
min-width: 0;
|
||||||
|
min-height: 420px;
|
||||||
|
max-height: calc(100vh - 220px);
|
||||||
|
padding: 16px 18px 18px;
|
||||||
|
}
|
||||||
|
.hub-logs-card-head {
|
||||||
|
display: flex;
|
||||||
|
align-items: baseline;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 10px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
.hub-logs-card-title {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
}
|
||||||
|
.hub-logs-card-hint {
|
||||||
|
font-size: 0.72rem;
|
||||||
|
color: var(--muted, #8892b0);
|
||||||
|
}
|
||||||
|
.hub-logs-pre {
|
||||||
|
flex: 1 1 auto;
|
||||||
|
min-height: 0;
|
||||||
|
overflow: auto;
|
||||||
|
margin: 0;
|
||||||
|
padding: 12px;
|
||||||
|
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
||||||
|
font-size: 0.74rem;
|
||||||
|
line-height: 1.45;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
word-break: break-word;
|
||||||
|
background: var(--inset-surface, #0d1018);
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 1px solid var(--border-soft);
|
||||||
|
color: #d8deef;
|
||||||
|
}
|
||||||
|
@media (max-width: 960px) {
|
||||||
|
.hub-logs-layout {
|
||||||
|
grid-template-columns: minmax(0, 1fr);
|
||||||
|
}
|
||||||
|
.hub-logs-card {
|
||||||
|
max-height: none;
|
||||||
|
min-height: 280px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,10 @@
|
|||||||
return displayPref("show_nav_strategy", true);
|
return displayPref("show_nav_strategy", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function showNavLogsPref() {
|
||||||
|
return displayPref("show_nav_logs", true);
|
||||||
|
}
|
||||||
|
|
||||||
function syncNavVisibility(data) {
|
function syncNavVisibility(data) {
|
||||||
const d = (data && data.display) || {};
|
const d = (data && data.display) || {};
|
||||||
const navFunds = document.getElementById("nav-funds");
|
const navFunds = document.getElementById("nav-funds");
|
||||||
@@ -50,6 +54,7 @@
|
|||||||
const navAi = document.getElementById("nav-ai");
|
const navAi = document.getElementById("nav-ai");
|
||||||
const navCalc = document.getElementById("nav-calculator");
|
const navCalc = document.getElementById("nav-calculator");
|
||||||
const navStrategy = document.getElementById("nav-strategy");
|
const navStrategy = document.getElementById("nav-strategy");
|
||||||
|
const navLogs = document.getElementById("nav-logs");
|
||||||
if (navFunds) navFunds.classList.toggle("nav-hidden", d.show_nav_funds === false);
|
if (navFunds) navFunds.classList.toggle("nav-hidden", d.show_nav_funds === false);
|
||||||
if (navDash) navDash.classList.toggle("nav-hidden", d.show_nav_dashboard === false);
|
if (navDash) navDash.classList.toggle("nav-hidden", d.show_nav_dashboard === false);
|
||||||
if (navPlan) navPlan.classList.toggle("nav-hidden", d.show_nav_plan === false);
|
if (navPlan) navPlan.classList.toggle("nav-hidden", d.show_nav_plan === false);
|
||||||
@@ -57,6 +62,7 @@
|
|||||||
if (navAi) navAi.classList.toggle("nav-hidden", d.show_nav_ai === false);
|
if (navAi) navAi.classList.toggle("nav-hidden", d.show_nav_ai === false);
|
||||||
if (navCalc) navCalc.classList.toggle("nav-hidden", d.show_nav_calculator === false);
|
if (navCalc) navCalc.classList.toggle("nav-hidden", d.show_nav_calculator === false);
|
||||||
if (navStrategy) navStrategy.classList.toggle("nav-hidden", d.show_nav_strategy === false);
|
if (navStrategy) navStrategy.classList.toggle("nav-hidden", d.show_nav_strategy === false);
|
||||||
|
if (navLogs) navLogs.classList.toggle("nav-hidden", d.show_nav_logs === false);
|
||||||
}
|
}
|
||||||
|
|
||||||
function pageNavAllowed(page) {
|
function pageNavAllowed(page) {
|
||||||
@@ -67,6 +73,7 @@
|
|||||||
if (page === "ai") return showNavAiPref();
|
if (page === "ai") return showNavAiPref();
|
||||||
if (page === "calculator") return showNavCalculatorPref();
|
if (page === "calculator") return showNavCalculatorPref();
|
||||||
if (page === "strategy") return showNavStrategyPref();
|
if (page === "strategy") return showNavStrategyPref();
|
||||||
|
if (page === "logs") return showNavLogsPref();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,6 +87,7 @@
|
|||||||
const aiCb = document.getElementById("pref-show-nav-ai");
|
const aiCb = document.getElementById("pref-show-nav-ai");
|
||||||
const calcCb = document.getElementById("pref-show-nav-calculator");
|
const calcCb = document.getElementById("pref-show-nav-calculator");
|
||||||
const strategyCb = document.getElementById("pref-show-nav-strategy");
|
const strategyCb = document.getElementById("pref-show-nav-strategy");
|
||||||
|
const logsCb = document.getElementById("pref-show-nav-logs");
|
||||||
if (pnlCb) pnlCb.checked = d.show_account_pnl !== false;
|
if (pnlCb) pnlCb.checked = d.show_account_pnl !== false;
|
||||||
if (fundsCb) fundsCb.checked = d.show_nav_funds !== false;
|
if (fundsCb) fundsCb.checked = d.show_nav_funds !== false;
|
||||||
if (dashCb) dashCb.checked = d.show_nav_dashboard !== false;
|
if (dashCb) dashCb.checked = d.show_nav_dashboard !== false;
|
||||||
@@ -88,6 +96,7 @@
|
|||||||
if (aiCb) aiCb.checked = d.show_nav_ai !== false;
|
if (aiCb) aiCb.checked = d.show_nav_ai !== false;
|
||||||
if (calcCb) calcCb.checked = d.show_nav_calculator !== false;
|
if (calcCb) calcCb.checked = d.show_nav_calculator !== false;
|
||||||
if (strategyCb) strategyCb.checked = d.show_nav_strategy !== false;
|
if (strategyCb) strategyCb.checked = d.show_nav_strategy !== false;
|
||||||
|
if (logsCb) logsCb.checked = d.show_nav_logs !== false;
|
||||||
syncNavVisibility(data);
|
syncNavVisibility(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1200,6 +1209,7 @@
|
|||||||
if (p.includes("plan")) return "plan";
|
if (p.includes("plan")) return "plan";
|
||||||
if (p.includes("calculator")) return "calculator";
|
if (p.includes("calculator")) return "calculator";
|
||||||
if (p.includes("strategy")) return "strategy";
|
if (p.includes("strategy")) return "strategy";
|
||||||
|
if (p.includes("logs")) return "logs";
|
||||||
if (p.includes("market")) return "market";
|
if (p.includes("market")) return "market";
|
||||||
if (p.includes("/ai")) return "ai";
|
if (p.includes("/ai")) return "ai";
|
||||||
return "monitor";
|
return "monitor";
|
||||||
@@ -1213,6 +1223,7 @@
|
|||||||
if (page === "plan") return "page-plan";
|
if (page === "plan") return "page-plan";
|
||||||
if (page === "calculator") return "page-calculator";
|
if (page === "calculator") return "page-calculator";
|
||||||
if (page === "strategy") return "page-strategy";
|
if (page === "strategy") return "page-strategy";
|
||||||
|
if (page === "logs") return "page-logs";
|
||||||
if (page === "market") return "page-market";
|
if (page === "market") return "page-market";
|
||||||
if (page === "ai") return "page-ai";
|
if (page === "ai") return "page-ai";
|
||||||
return "page-monitor";
|
return "page-monitor";
|
||||||
@@ -1264,13 +1275,18 @@
|
|||||||
}
|
}
|
||||||
if (page === "funds" && window.hubFundsPage) {
|
if (page === "funds" && window.hubFundsPage) {
|
||||||
window.hubFundsPage.init();
|
window.hubFundsPage.init();
|
||||||
|
} else if (window.hubFundsPage && window.hubFundsPage.destroy) {
|
||||||
|
window.hubFundsPage.destroy();
|
||||||
}
|
}
|
||||||
if (page === "strategy" && window.hubStrategyPage) {
|
if (page === "strategy" && window.hubStrategyPage) {
|
||||||
window.hubStrategyPage.init();
|
window.hubStrategyPage.init();
|
||||||
} else if (window.hubStrategyPage && window.hubStrategyPage.destroy) {
|
} else if (window.hubStrategyPage && window.hubStrategyPage.destroy) {
|
||||||
window.hubStrategyPage.destroy();
|
window.hubStrategyPage.destroy();
|
||||||
} else if (window.hubFundsPage && window.hubFundsPage.destroy) {
|
}
|
||||||
window.hubFundsPage.destroy();
|
if (page === "logs" && window.hubLogsPage) {
|
||||||
|
window.hubLogsPage.init();
|
||||||
|
} else if (window.hubLogsPage && window.hubLogsPage.destroy) {
|
||||||
|
window.hubLogsPage.destroy();
|
||||||
}
|
}
|
||||||
if (page === "market" && window.hubMarketChart) {
|
if (page === "market" && window.hubMarketChart) {
|
||||||
window.hubMarketChart.init();
|
window.hubMarketChart.init();
|
||||||
@@ -4667,6 +4683,7 @@
|
|||||||
const aiCb = document.getElementById("pref-show-nav-ai");
|
const aiCb = document.getElementById("pref-show-nav-ai");
|
||||||
const calcCb = document.getElementById("pref-show-nav-calculator");
|
const calcCb = document.getElementById("pref-show-nav-calculator");
|
||||||
const strategyCb = document.getElementById("pref-show-nav-strategy");
|
const strategyCb = document.getElementById("pref-show-nav-strategy");
|
||||||
|
const logsCb = document.getElementById("pref-show-nav-logs");
|
||||||
const supEnabled = document.getElementById("supervisor-enabled");
|
const supEnabled = document.getElementById("supervisor-enabled");
|
||||||
const supProg = document.getElementById("supervisor-wechat-program");
|
const supProg = document.getElementById("supervisor-wechat-program");
|
||||||
const supWebhook = document.getElementById("supervisor-wechat-webhook");
|
const supWebhook = document.getElementById("supervisor-wechat-webhook");
|
||||||
@@ -4687,6 +4704,7 @@
|
|||||||
show_nav_ai: aiCb ? !!aiCb.checked : true,
|
show_nav_ai: aiCb ? !!aiCb.checked : true,
|
||||||
show_nav_calculator: calcCb ? !!calcCb.checked : true,
|
show_nav_calculator: calcCb ? !!calcCb.checked : true,
|
||||||
show_nav_strategy: strategyCb ? !!strategyCb.checked : true,
|
show_nav_strategy: strategyCb ? !!strategyCb.checked : true,
|
||||||
|
show_nav_logs: logsCb ? !!logsCb.checked : true,
|
||||||
},
|
},
|
||||||
supervisor: {
|
supervisor: {
|
||||||
enabled: supEnabled ? !!supEnabled.checked : true,
|
enabled: supEnabled ? !!supEnabled.checked : true,
|
||||||
|
|||||||
@@ -57,6 +57,7 @@
|
|||||||
<a href="/archive" id="nav-archive">内照明心</a>
|
<a href="/archive" id="nav-archive">内照明心</a>
|
||||||
<a href="/dashboard" id="nav-dashboard">数据看板</a>
|
<a href="/dashboard" id="nav-dashboard">数据看板</a>
|
||||||
<a href="/ai" id="nav-ai">AI 教练</a>
|
<a href="/ai" id="nav-ai">AI 教练</a>
|
||||||
|
<a href="/logs" id="nav-logs">系统日志</a>
|
||||||
<a href="/settings" id="nav-settings">系统设置</a>
|
<a href="/settings" id="nav-settings">系统设置</a>
|
||||||
</nav>
|
</nav>
|
||||||
<button type="button" id="btn-logout" class="ghost" title="退出登录">退出</button>
|
<button type="button" id="btn-logout" class="ghost" title="退出登录">退出</button>
|
||||||
@@ -882,6 +883,39 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div id="page-logs" class="page hidden">
|
||||||
|
<div class="page-head hub-logs-page-head">
|
||||||
|
<div>
|
||||||
|
<h1><span class="head-tag">LOG</span> 系统日志</h1>
|
||||||
|
<p class="page-desc">三所实例与中控 PM2 进程日志 · 实时输出与报错分离展示</p>
|
||||||
|
</div>
|
||||||
|
<div class="hub-logs-page-actions">
|
||||||
|
<button type="button" id="hub-logs-btn-refresh" class="ghost">立即刷新</button>
|
||||||
|
<button type="button" id="hub-logs-btn-pause" class="ghost">暂停刷新</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="hub-logs-toolbar">
|
||||||
|
<div id="hub-logs-tabs" class="hub-logs-tabs" role="tablist" aria-label="日志来源"></div>
|
||||||
|
<span id="hub-logs-status" class="toolbar-meta"></span>
|
||||||
|
</div>
|
||||||
|
<div class="hub-logs-layout">
|
||||||
|
<section class="card hub-logs-card">
|
||||||
|
<div class="hub-logs-card-head">
|
||||||
|
<h3 class="hub-logs-card-title">实时日志</h3>
|
||||||
|
<span class="hub-logs-card-hint">stdout</span>
|
||||||
|
</div>
|
||||||
|
<pre id="hub-logs-out" class="hub-logs-pre">加载中…</pre>
|
||||||
|
</section>
|
||||||
|
<section class="card hub-logs-card">
|
||||||
|
<div class="hub-logs-card-head">
|
||||||
|
<h3 class="hub-logs-card-title">报错日志</h3>
|
||||||
|
<span class="hub-logs-card-hint">stderr</span>
|
||||||
|
</div>
|
||||||
|
<pre id="hub-logs-err" class="hub-logs-pre">加载中…</pre>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id="page-settings" class="page hidden">
|
<div id="page-settings" class="page hidden">
|
||||||
<div class="page-head">
|
<div class="page-head">
|
||||||
<h1><span class="head-tag">CFG</span> 系统设置</h1>
|
<h1><span class="head-tag">CFG</span> 系统设置</h1>
|
||||||
@@ -969,6 +1003,10 @@
|
|||||||
<input type="checkbox" id="pref-show-nav-strategy" checked />
|
<input type="checkbox" id="pref-show-nav-strategy" checked />
|
||||||
顶栏显示「策略说明」
|
顶栏显示「策略说明」
|
||||||
</label>
|
</label>
|
||||||
|
<label class="chk-label settings-display-chk">
|
||||||
|
<input type="checkbox" id="pref-show-nav-logs" checked />
|
||||||
|
顶栏显示「系统日志」
|
||||||
|
</label>
|
||||||
<p class="settings-display-hint">保存至 hub_settings.json,换浏览器同样生效.关闭导航后对应页面将不可从顶栏进入,直接访问 URL 会跳回监控区.</p>
|
<p class="settings-display-hint">保存至 hub_settings.json,换浏览器同样生效.关闭导航后对应页面将不可从顶栏进入,直接访问 URL 会跳回监控区.</p>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@@ -1188,6 +1226,7 @@
|
|||||||
<script src="/assets/funds.js?v=20260707-hub-options-funds"></script>
|
<script src="/assets/funds.js?v=20260707-hub-options-funds"></script>
|
||||||
<script src="/assets/dashboard.js?v=20260708-options-expiry-cd"></script>
|
<script src="/assets/dashboard.js?v=20260708-options-expiry-cd"></script>
|
||||||
<script src="/assets/strategy.js?v=3"></script>
|
<script src="/assets/strategy.js?v=3"></script>
|
||||||
|
<script src="/assets/logs.js?v=1"></script>
|
||||||
<script src="/assets/ai_review_render.js?v=3"></script>
|
<script src="/assets/ai_review_render.js?v=3"></script>
|
||||||
<script src="/assets/time_close_ui.js?v=3"></script>
|
<script src="/assets/time_close_ui.js?v=3"></script>
|
||||||
<script src="/assets/options_expiry_countdown.js?v=1"></script>
|
<script src="/assets/options_expiry_countdown.js?v=1"></script>
|
||||||
|
|||||||
@@ -0,0 +1,169 @@
|
|||||||
|
/**
|
||||||
|
* 系统日志:三所 + 中控 PM2 stdout/stderr.
|
||||||
|
*/
|
||||||
|
(function () {
|
||||||
|
const page = document.getElementById("page-logs");
|
||||||
|
if (!page) return;
|
||||||
|
|
||||||
|
const tabsEl = document.getElementById("hub-logs-tabs");
|
||||||
|
const statusEl = document.getElementById("hub-logs-status");
|
||||||
|
const outEl = document.getElementById("hub-logs-out");
|
||||||
|
const errEl = document.getElementById("hub-logs-err");
|
||||||
|
const btnRefresh = document.getElementById("hub-logs-btn-refresh");
|
||||||
|
const btnPause = document.getElementById("hub-logs-btn-pause");
|
||||||
|
|
||||||
|
const POLL_MS = 4000;
|
||||||
|
let activeKey = "binance";
|
||||||
|
let tabsMeta = [];
|
||||||
|
let pollTimer = null;
|
||||||
|
let paused = false;
|
||||||
|
let loading = false;
|
||||||
|
let bound = false;
|
||||||
|
|
||||||
|
async function apiFetch(url) {
|
||||||
|
const r = await fetch(url, { credentials: "same-origin" });
|
||||||
|
const ct = (r.headers.get("content-type") || "").toLowerCase();
|
||||||
|
if (ct.includes("application/json")) {
|
||||||
|
const data = await r.json();
|
||||||
|
if (!r.ok) throw new Error((data && (data.msg || data.detail)) || r.statusText || "请求失败");
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
if (!r.ok) throw new Error(r.statusText || "请求失败");
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
function esc(s) {
|
||||||
|
return String(s ?? "")
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")
|
||||||
|
.replace(/"/g, """);
|
||||||
|
}
|
||||||
|
|
||||||
|
function wasScrolledToBottom(el) {
|
||||||
|
if (!el) return true;
|
||||||
|
return el.scrollHeight - el.scrollTop - el.clientHeight < 24;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setPreText(el, text, stickBottom) {
|
||||||
|
if (!el) return;
|
||||||
|
const atBottom = stickBottom || wasScrolledToBottom(el);
|
||||||
|
el.textContent = text || "(暂无日志)";
|
||||||
|
if (atBottom) el.scrollTop = el.scrollHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setStatus(text, isErr) {
|
||||||
|
if (!statusEl) return;
|
||||||
|
statusEl.textContent = text || "";
|
||||||
|
statusEl.classList.toggle("is-err", !!isErr);
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderTabs() {
|
||||||
|
if (!tabsEl) return;
|
||||||
|
tabsEl.innerHTML = tabsMeta
|
||||||
|
.map(
|
||||||
|
(t) =>
|
||||||
|
`<button type="button" class="hub-logs-tab${t.key === activeKey ? " is-active" : ""}" role="tab" aria-selected="${t.key === activeKey}" data-key="${esc(t.key)}">${esc(t.label)}</button>`
|
||||||
|
)
|
||||||
|
.join("");
|
||||||
|
tabsEl.querySelectorAll(".hub-logs-tab").forEach((btn) => {
|
||||||
|
btn.addEventListener("click", () => {
|
||||||
|
const key = btn.getAttribute("data-key");
|
||||||
|
if (!key || key === activeKey) return;
|
||||||
|
activeKey = key;
|
||||||
|
renderTabs();
|
||||||
|
void loadLogs(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadMeta() {
|
||||||
|
const meta = await apiFetch("/api/system-logs/meta");
|
||||||
|
tabsMeta = Array.isArray(meta.targets) ? meta.targets : [];
|
||||||
|
if (tabsMeta.length && !tabsMeta.some((t) => t.key === activeKey)) {
|
||||||
|
activeKey = tabsMeta[0].key;
|
||||||
|
}
|
||||||
|
renderTabs();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadLogs(force) {
|
||||||
|
if (loading && !force) return;
|
||||||
|
loading = true;
|
||||||
|
try {
|
||||||
|
const data = await apiFetch(`/api/system-logs/${encodeURIComponent(activeKey)}?lines=200`);
|
||||||
|
setPreText(outEl, data.out || "", true);
|
||||||
|
setPreText(errEl, data.err || "", true);
|
||||||
|
const ts = data.updated_at ? new Date(data.updated_at * 1000) : new Date();
|
||||||
|
const hh = String(ts.getHours()).padStart(2, "0");
|
||||||
|
const mm = String(ts.getMinutes()).padStart(2, "0");
|
||||||
|
const ss = String(ts.getSeconds()).padStart(2, "0");
|
||||||
|
const missing = [];
|
||||||
|
if (!data.out_exists) missing.push("实时");
|
||||||
|
if (!data.err_exists) missing.push("报错");
|
||||||
|
const hint = missing.length ? ` · ${missing.join("/")}日志文件暂无` : "";
|
||||||
|
setStatus(`已更新 ${hh}:${mm}:${ss}${hint}`, false);
|
||||||
|
} catch (e) {
|
||||||
|
setStatus(e.message || "加载失败", true);
|
||||||
|
} finally {
|
||||||
|
loading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function startPoll() {
|
||||||
|
stopPoll();
|
||||||
|
if (paused) return;
|
||||||
|
pollTimer = window.setInterval(() => {
|
||||||
|
void loadLogs(false);
|
||||||
|
}, POLL_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopPoll() {
|
||||||
|
if (pollTimer) {
|
||||||
|
clearInterval(pollTimer);
|
||||||
|
pollTimer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function bindControls() {
|
||||||
|
if (bound) return;
|
||||||
|
bound = true;
|
||||||
|
if (btnRefresh) {
|
||||||
|
btnRefresh.addEventListener("click", () => {
|
||||||
|
void loadLogs(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (btnPause) {
|
||||||
|
btnPause.addEventListener("click", () => {
|
||||||
|
paused = !paused;
|
||||||
|
btnPause.textContent = paused ? "继续刷新" : "暂停刷新";
|
||||||
|
btnPause.classList.toggle("is-paused", paused);
|
||||||
|
if (paused) stopPoll();
|
||||||
|
else startPoll();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function init() {
|
||||||
|
bindControls();
|
||||||
|
paused = false;
|
||||||
|
if (btnPause) {
|
||||||
|
btnPause.textContent = "暂停刷新";
|
||||||
|
btnPause.classList.remove("is-paused");
|
||||||
|
}
|
||||||
|
setStatus("加载中…", false);
|
||||||
|
try {
|
||||||
|
await loadMeta();
|
||||||
|
await loadLogs(true);
|
||||||
|
startPoll();
|
||||||
|
} catch (e) {
|
||||||
|
setStatus(e.message || "初始化失败", true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function destroy() {
|
||||||
|
stopPoll();
|
||||||
|
setStatus("", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.hubLogsPage = { init, destroy };
|
||||||
|
})();
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
"""hub_system_logs_lib 单元测试."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from lib.hub.hub_system_logs_lib import (
|
||||||
|
load_system_logs,
|
||||||
|
log_file_paths,
|
||||||
|
system_logs_meta,
|
||||||
|
tail_lines,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class HubSystemLogsLibTest(unittest.TestCase):
|
||||||
|
def test_system_logs_meta(self):
|
||||||
|
meta = system_logs_meta()
|
||||||
|
self.assertTrue(meta["ok"])
|
||||||
|
keys = [t["key"] for t in meta["targets"]]
|
||||||
|
self.assertEqual(keys, ["binance", "gate", "okx", "hub"])
|
||||||
|
|
||||||
|
def test_tail_lines_reads_last_lines(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
path = Path(tmp) / "demo-out.log"
|
||||||
|
path.write_text("\n".join(f"line-{i}" for i in range(1, 11)), encoding="utf-8")
|
||||||
|
out = tail_lines(path, lines=3)
|
||||||
|
self.assertEqual(out.splitlines(), ["line-8", "line-9", "line-10"])
|
||||||
|
|
||||||
|
def test_load_system_logs_unknown(self):
|
||||||
|
with self.assertRaises(KeyError):
|
||||||
|
load_system_logs("unknown")
|
||||||
|
|
||||||
|
def test_load_system_logs_missing_files(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
logs_dir = Path(tmp)
|
||||||
|
out_path, err_path = log_file_paths("crypto_gate")
|
||||||
|
# monkeypatch by writing into expected structure under temp is hard;
|
||||||
|
# just verify shape with real call (files may be absent on dev machine).
|
||||||
|
payload = load_system_logs("gate", lines=50)
|
||||||
|
self.assertTrue(payload["ok"])
|
||||||
|
self.assertEqual(payload["key"], "gate")
|
||||||
|
self.assertIn("out", payload)
|
||||||
|
self.assertIn("err", payload)
|
||||||
|
self.assertIsInstance(payload["out"], str)
|
||||||
|
self.assertIsInstance(payload["err"], str)
|
||||||
|
_ = out_path, err_path
|
||||||
Reference in New Issue
Block a user