优化日志解析逻辑,添加倒序插入和日志数量限制

This commit is contained in:
jxxghp
2025-01-15 22:03:28 +08:00
parent 761e3ac76d
commit e60015a477
+13 -7
View File
@@ -1,4 +1,6 @@
<script lang="ts" setup> <script lang="ts" setup>
import { parse } from 'path'
// 日志列表 // 日志列表
const logs = ref<string[]>([]) const logs = ref<string[]>([])
@@ -18,8 +20,8 @@ let eventSource: EventSource | null = null
// 日志颜色映射表 // 日志颜色映射表
const logColorMap: Record<string, string> = { const logColorMap: Record<string, string> = {
DEBUG: 'primary', DEBUG: 'secondary',
INFO: 'secondary', INFO: 'info',
WARNING: 'warning', WARNING: 'warning',
ERROR: 'error', ERROR: 'error',
} }
@@ -44,18 +46,18 @@ function startSSELogging() {
logs.value.push(...buffer) logs.value.push(...buffer)
buffer.length = 0 buffer.length = 0
timeoutId = null timeoutId = null
}, 100) // 批量处理间隔,视需求调整 }, 100)
} }
} }
}) })
} }
// 解析日志 // 解析日志并倒序
watch( watch(
logs, logs,
newLogs => { newLogs => {
// 解析新日志
const newParsedLogs = newLogs const newParsedLogs = newLogs
.slice(parsedLogs.value.length)
.map(log => { .map(log => {
const logPattern = /^【(.*?)】[0-9\-:]*\s(.*?)\s-\s(.*?)\s-\s(.*)$/ const logPattern = /^【(.*?)】[0-9\-:]*\s(.*?)\s-\s(.*?)\s-\s(.*)$/
const matches = log.match(logPattern) const matches = log.match(logPattern)
@@ -66,10 +68,14 @@ watch(
return null return null
}) })
.filter(Boolean) .filter(Boolean)
parsedLogs.value.push(...(newParsedLogs as any[]))
// 倒序后插入parsedLogs顶部
parsedLogs.value.unshift(...(newParsedLogs.reverse() as any[]))
// 只保留最新的1000条日志
parsedLogs.value = parsedLogs.value.slice(0, 1000)
}, },
{ deep: true }, { deep: true },
) // 添加 deep 监听,确保 Vue 响应式正确触发 )
onMounted(() => { onMounted(() => {
startSSELogging() startSSELogging()