fix: historyLog

This commit is contained in:
手瓜一十雪
2024-12-05 19:37:56 +08:00
committed by 纸凤孤凰
parent 1a533742a5
commit 9aec3865ff
2 changed files with 43 additions and 16 deletions

View File

@@ -33,7 +33,9 @@
</t-button> </t-button>
</t-tooltip> </t-tooltip>
</t-col> </t-col>
<t-col v-if="LogDataType === 'history'" flex="auto" <t-col
v-if="LogDataType === 'history'"
flex="auto"
style="display: inline-flex; justify-content: center"> style="display: inline-flex; justify-content: center">
<t-tooltip content="历史日志"> <t-tooltip content="历史日志">
<t-button variant="text" shape="square" @click="historyLog"> <t-button variant="text" shape="square" @click="historyLog">
@@ -168,6 +170,7 @@ const optValue = ref<OptionItem>({
const openTypeList = (data: OptionItem) => { const openTypeList = (data: OptionItem) => {
optValue.value = data; optValue.value = data;
}; };
const logType = ['debug', 'info', 'warn', 'error', 'fatal'];
//清理log //清理log
const clearLogs = () => { const clearLogs = () => {
if (LogDataType.value === 'realtime') { if (LogDataType.value === 'realtime') {
@@ -218,9 +221,21 @@ const stopTimer = () => {
intervalId.value = null; intervalId.value = null;
} }
}; };
const extractContent = (text: string): string | null => {
const regex = /\[([^\]]+)]/;
const match = regex.exec(text);
if (match && match[1]) {
const extracted = match[1].toLowerCase();
if (logType.includes(extracted)) {
return match[1];
}
}
return null;
};
const loadData = (text: string, loadType: string) => { const loadData = (text: string, loadType: string) => {
const lines = text.split(/\r\n/); const lines = text.split(/\r\n/);
lines.forEach((line) => { lines.forEach((line) => {
if (loadType === 'realtime') {
let remoteJson = JSON.parse(line) as { message: string, level: string }; let remoteJson = JSON.parse(line) as { message: string, level: string };
const type = remoteJson.level; const type = remoteJson.level;
const actualType = type || 'other'; const actualType = type || 'other';
@@ -231,10 +246,20 @@ const loadData = (text: string, loadType: string) => {
color: color, color: color,
time: '', time: '',
}; };
if (loadType === 'realtime') {
updateLogList(realtimeLogHtmlList, actualType, data); updateLogList(realtimeLogHtmlList, actualType, data);
} else if (loadType === 'history') { } else if (loadType === 'history') {
const type = extractContent(line);
const actualType = type || 'other';
const timeRegex = /(\d{2}-\d{2} \d{2}:\d{2}:\d{2})|(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/;
const match = timeRegex.exec(line);
let time = match ? match[0] : null;
const color = actualType && typeKey.value[actualType] ? typeKey.value[actualType] : undefined;
const data: logHtml = {
type: actualType,
content: line.slice(match ? match[0].length : 0) || '',
color: color,
time: time ? time + ' ' : '',
};
updateLogList(historyLogHtmlList, actualType, data); updateLogList(historyLogHtmlList, actualType, data);
} }
}); });
@@ -245,7 +270,7 @@ const updateLogList = (logList: Ref<Map<string, Array<logHtml>>>, actualType: st
if (Array.isArray(allLogs)) { if (Array.isArray(allLogs)) {
allLogs.push(data); allLogs.push(data);
} }
if (actualType !== 'unknown') { if (actualType !== 'other') {
const typeLogs = logList.value.get(actualType); const typeLogs = logList.value.get(actualType);
if (Array.isArray(typeLogs)) { if (Array.isArray(typeLogs)) {
typeLogs.push(data); typeLogs.push(data);
@@ -338,6 +363,7 @@ const fetchRealTimeLogs = async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-expect-error //@ts-expect-error
eventSource.value.onmessage = (event: MessageEvent) => { eventSource.value.onmessage = (event: MessageEvent) => {
console.log(event.data)
loadData(event.data, 'realtime'); loadData(event.data, 'realtime');
}; };
} }

View File

@@ -3,7 +3,8 @@ import type { RequestHandler } from 'express';
// CORS 中间件,跨域用 // CORS 中间件,跨域用
export const cors: RequestHandler = (_, res, next) => { export const cors: RequestHandler = (_, res, next) => {
res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); res.header('Access-Control-Allow-Methods', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization'); res.header('Access-Control-Allow-Headers', '*');
res.header('Access-Control-Allow-Credentials', 'true');
next(); next();
}; };