优化日志解析性能

This commit is contained in:
jxxghp
2025-01-16 19:51:58 +08:00
parent f021ba8a98
commit 1fe8aeb9e1
+18 -32
View File
@@ -1,9 +1,4 @@
<script lang="ts" setup> <script lang="ts" setup>
import { parse } from 'path'
// 日志列表
const logs = ref<string[]>([])
// 已解析的日志列表 // 已解析的日志列表
const parsedLogs = ref<{ level: string; time: string; program: string; content: string }[]>([]) const parsedLogs = ref<{ level: string; time: string; program: string; content: string }[]>([])
@@ -43,9 +38,23 @@ function startSSELogging() {
buffer.push(message) buffer.push(message)
if (!timeoutId) { if (!timeoutId) {
timeoutId = window.setTimeout(() => { timeoutId = window.setTimeout(() => {
logs.value.push(...buffer) // 解析新日志
// 限制长度为1000 const newParsedLogs = buffer
logs.value = logs.value.slice(-1000) .map(log => {
const logPattern = /^【(.*?)】[0-9\-:]*\s(.*?)\s-\s(.*?)\s-\s(.*)$/
const matches = log.match(logPattern)
if (matches) {
const [, level, time, program, content] = matches
return { level, time, program, content }
}
return null
})
.filter(Boolean)
// 倒序后插入parsedLogs顶部
parsedLogs.value.unshift(...(newParsedLogs.reverse() as any[]))
// 保留最新的200条日志
parsedLogs.value = parsedLogs.value.slice(0, 200)
// 重置buffer
buffer.length = 0 buffer.length = 0
timeoutId = null timeoutId = null
}, 100) }, 100)
@@ -54,29 +63,6 @@ function startSSELogging() {
}) })
} }
// 解析日志并倒序
watch(
logs,
newLogs => {
// 解析新日志
const newParsedLogs = newLogs
.map(log => {
const logPattern = /^【(.*?)】[0-9\-:]*\s(.*?)\s-\s(.*?)\s-\s(.*)$/
const matches = log.match(logPattern)
if (matches) {
const [, level, time, program, content] = matches
return { level, time, program, content }
}
return null
})
.filter(Boolean)
// 倒序后插入parsedLogs顶部
parsedLogs.value = [...(newParsedLogs.reverse() as any[])]
},
{ deep: true },
)
onMounted(() => { onMounted(() => {
startSSELogging() startSSELogging()
}) })
@@ -87,7 +73,7 @@ onBeforeUnmount(() => {
</script> </script>
<template> <template>
<LoadingBanner v-if="logs.length === 0" class="mt-12" text="正在刷新 ..." /> <LoadingBanner v-if="parsedLogs.length === 0" class="mt-12" text="正在刷新 ..." />
<div v-else> <div v-else>
<VTable class="table-rounded" hide-default-footer disable-sort> <VTable class="table-rounded" hide-default-footer disable-sort>
<tbody> <tbody>