增加了日志功能,修复了bug

This commit is contained in:
zjk
2025-05-19 02:14:11 +08:00
parent 9e1819cd75
commit 50a496e3e2
4 changed files with 222 additions and 20 deletions
+38 -12
View File
@@ -373,17 +373,37 @@ function initWebSocket() {
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type === 'log') {
// 初始加载的日志数据
allLogs = data.content;
filterAndDisplayLogs();
} else if (data.type === 'new_log') {
// 新的日志条目
if (Array.isArray(data.content)) {
// 初始加载的日志数据
allLogs = data.content;
filterAndDisplayLogs();
} else {
// 新的日志条目
allLogs.push(data.content);
if (shouldShowLog(data.content)) {
appendLogEntry(data.content);
let shouldUpdate = false;
data.content.forEach(log => {
allLogs.push(log);
if (shouldShowLog(log)) {
filteredLogs.push(log);
shouldUpdate = true;
// 如果在最后一页或自动滚动模式下,直接添加到显示
if (currentPage === totalPages || autoScroll) {
appendLogEntry(log);
}
}
});
if (shouldUpdate) {
totalPages = Math.ceil(filteredLogs.length / pageSize);
updatePagination();
// 如果开启了自动滚动,确保滚动到底部
if (autoScroll) {
logContent.scrollTop = logContent.scrollHeight;
}
}
}
} else if (data.type === 'system_status') {
updateStatus(data.status, true);
}
updateTime();
};
@@ -391,7 +411,12 @@ function initWebSocket() {
ws.onclose = () => {
console.log('WebSocket连接已关闭');
updateStatus('已断开', false);
setTimeout(initWebSocket, 5000);
// 增加重连延迟,避免快速重连
setTimeout(() => {
if (!ws || ws.readyState === WebSocket.CLOSED) {
initWebSocket();
}
}, 2000);
};
ws.onerror = (error) => {
@@ -505,9 +530,9 @@ function appendLogEntry(content) {
entry.className = 'log-entry';
// 根据日志内容设置不同的样式
if (content.includes('ERROR')) {
if (content.includes('ERROR') || content.includes('ERR')) {
entry.classList.add('error');
} else if (content.includes('WARNING')) {
} else if (content.includes('WARNING') || content.includes('WARN')) {
entry.classList.add('warning');
} else if (content.includes('INFO')) {
entry.classList.add('info');
@@ -515,7 +540,8 @@ function appendLogEntry(content) {
entry.textContent = content;
logContent.appendChild(entry);
// 只在自动滚动模式下滚动到底部
if (autoScroll) {
logContent.scrollTop = logContent.scrollHeight;
}