优化日志读取与传输效率

This commit is contained in:
liuwei
2025-12-25 15:55:42 +08:00
parent 36d56a2a87
commit 672ea93201

View File

@@ -30,7 +30,7 @@
<div v-loading="loading" class="log-content-wrapper"> <div v-loading="loading" class="log-content-wrapper">
<div v-if="logContent && logContent.length > 0" class="log-content"> <div v-if="logContent && logContent.length > 0" class="log-content">
<pre>{% raw %}{{ logContent.join('') }}{% endraw %}</pre> <pre ref="logPre"></pre>
</div> </div>
<div v-else class="empty-log"> <div v-else class="empty-log">
<el-empty description="暂无日志内容"></el-empty> <el-empty description="暂无日志内容"></el-empty>
@@ -51,30 +51,60 @@
logType: 'info', logType: 'info',
logLines: 100, logLines: 100,
logContent: [], logContent: [],
logText: '',
refreshInterval: 0, refreshInterval: 0,
refreshTimer: null, refreshTimer: null,
currentView: '9', currentView: '9',
showTimeRangeSelector: false showTimeRangeSelector: false,
cancelSource: null,
isAutoScroll: true
} }
}, },
mounted() { mounted() {
this.loadLogs(); this.loadLogs();
this.$nextTick(() => {
const logDiv = this.$el.querySelector('.log-content');
if (logDiv) {
logDiv.addEventListener('scroll', () => {
const nearBottom = (logDiv.scrollHeight - logDiv.scrollTop - logDiv.clientHeight) < 50;
this.isAutoScroll = nearBottom;
});
}
});
}, },
beforeDestroy() { beforeDestroy() {
this.clearRefreshTimer(); this.clearRefreshTimer();
if (this.cancelSource) {
this.cancelSource.cancel('component destroyed');
this.cancelSource = null;
}
}, },
methods: { methods: {
loadLogs() { loadLogs() {
this.loading = true; this.loading = true;
axios.get(`/api/wx_logs?type=${this.logType}&lines=${this.logLines}`) if (this.cancelSource) {
this.cancelSource.cancel('new request');
}
this.cancelSource = axios.CancelToken.source();
axios.get(`/api/wx_logs`, {
params: { type: this.logType, lines: this.logLines },
headers: { 'Accept-Encoding': 'gzip' },
cancelToken: this.cancelSource.token
})
.then(response => { .then(response => {
if (response.data.success) { if (response.data.success) {
this.logContent = response.data.data.content || []; this.logContent = response.data.data.content || [];
this.logText = this.logContent.join('');
this.$nextTick(() => { this.$nextTick(() => {
const logDiv = this.$el.querySelector('.log-content'); const logDiv = this.$el.querySelector('.log-content');
const pre = this.$refs.logPre;
if (pre) {
pre.textContent = this.logText;
}
if (logDiv) { if (logDiv) {
logDiv.scrollTop = logDiv.scrollHeight; if (this.isAutoScroll) {
console.log('log-content scrollHeight:', logDiv.scrollHeight, 'clientHeight:', logDiv.clientHeight); logDiv.scrollTop = logDiv.scrollHeight;
}
} }
}); });
} else { } else {
@@ -82,8 +112,10 @@
} }
}) })
.catch(error => { .catch(error => {
console.error('加载日志出错:', error); if (!axios.isCancel(error)) {
this.$message.error('加载日志出错'); console.error('加载日志出错:', error);
this.$message.error('加载日志出错');
}
}) })
.finally(() => { .finally(() => {
this.loading = false; this.loading = false;