Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(logs): refactor log delivery and resolve loading and scrolling issues

Timothy Yin6d2ed6980a5f
diff --git a/app/pages/dashboard/logs.vue b/app/pages/dashboard/logs.vue
index 1a459728..6718727e 100644
--- a/app/pages/dashboard/logs.vue
+++ b/app/pages/dashboard/logs.vue
@@ -15,33 +15,47 @@ interface LogEntry {
raw: string
}
-const logs = ref<LogEntry[]>([])
+const logs = shallowRef<LogEntry[]>([])
const searchQuery = ref('')
const selectedLevels = ref<string[]>([])
const selectedTags = ref<string[]>([])
const autoScroll = ref(true)
const isConnected = ref(false)
-const connectionState = ref<'idle' | 'connecting' | 'loadingHistory' | 'live' | 'error'>('idle')
+const connectionState = ref<
+ 'idle' | 'connecting' | 'loadingHistory' | 'live' | 'error'
+>('idle')
const logContainer = ref<HTMLElement>()
const isInitialLoading = ref(false)
-const loadingProgress = ref(0)
const normalizedSearchQuery = computed(() =>
searchQuery.value.trim().toLowerCase(),
)
const scrollTop = ref(0)
const containerHeight = ref(0)
-
-// 批处理队列
-const batchQueue = ref<LogEntry[]>([])
-const isBatchProcessing = ref(false)
const MAX_LOG_LINES = 6000
-const TRIM_TO_LOG_LINES = 4000
-const BATCH_SIZE = 100 // 每批处理的日志条数
-const BATCH_DELAY = 8 // 每批处理间隔(毫秒)
-const INITIAL_LOG_LINES = 'all'
+const historyCursor = ref(0)
+const isLoadingOlder = ref(false)
+let liveOffset = 0
+let fileIdentity = ''
+let requestController: AbortController | null = null
+let reconnectTimer: ReturnType<typeof setTimeout> | null = null
+let disposed = false
+let isPositioningInitialLogs = false
+let smoothScrollTarget: number | null = null
+const cancelSmoothScroll = () => {
+ smoothScrollTarget = null
+}
+
+interface LogPage {
+ lines: string[]
+ before: number
+ offset: number
+ identity: string
+}
+
const ROW_HEIGHT = 28
const VIRTUAL_OVERSCAN = 20
const VIRTUAL_BOTTOM_PADDING = 8
+const HISTORY_LOAD_THRESHOLD = ROW_HEIGHT * 10
let resizeObserver: ResizeObserver | null = null
@@ -62,53 +76,6 @@ const getLevelType = (level: number): string => {
// EventSource 连接
let eventSource: EventSource | null = null
-// 批处理添加日志
-const addLogEntry = (logEntry: LogEntry) => {
- batchQueue.value.push(logEntry)
- if (!isBatchProcessing.value) {
- processBatch()
- }
-}
-
-// 处理批队列
-const processBatch = async () => {
- if (isBatchProcessing.value || batchQueue.value.length === 0) return
-
- isBatchProcessing.value = true
-
- while (batchQueue.value.length > 0) {
- const batch = batchQueue.value.splice(0, BATCH_SIZE)
- logs.value.push(...batch)
-
- // 限制日志条数,避免内存泄漏
- if (logs.value.length > MAX_LOG_LINES) {
- logs.value = logs.value.slice(-TRIM_TO_LOG_LINES)
- }
-
- // 更新加载进度(只在初始加载时显示)
- if (isInitialLoading.value) {
- loadingProgress.value = Math.min(
- 95,
- loadingProgress.value + batch.length * 0.05,
- )
- }
-
- // 自动滚动到底部
- if (autoScroll.value) {
- await scrollToBottom()
- }
-
- // 如果还有更多批次,延迟处理以避免阻塞UI
- if (batchQueue.value.length > 0) {
- await new Promise((resolve) => setTimeout(resolve, BATCH_DELAY))
- }
- }
-
- isBatchProcessing.value = false
-
- // 注意:初始加载完成现在由消息超时检测控制,不在这里处理
-}
-
// 解析日志行
const parseLogLine = (line: string): LogEntry | null => {
try {
@@ -127,7 +94,7 @@ const parseLogLine = (line: string): LogEntry | null => {
message,
messageLower: message.toLowerCase(),
type: logData.type || 'info',
- level: logData.level || 3,
+ level: logData.level ?? 3,
tag,
tagLower: tag.toLowerCase(),
raw: line,
@@ -178,6 +145,34 @@ const filteredLogs = computed(() => {
return filtered
})
+const hasActiveFilters = computed(() =>
+ Boolean(
+ normalizedSearchQuery.value ||
+ selectedLevels.value.length ||
+ selectedTags.value.length,
+ ),
+)
+
+// Summarize the loaded window, rather than implying a count of the full archive.
+const logSummary = computed(() => {
+ let errors = 0
+ let warnings = 0
+ for (const log of logs.value) {
+ const type = log.type || getLevelType(log.level)
+ if (type === 'error') errors++
+ if (type === 'warn') warnings++
+ }
+ const first = logs.value[0]?.date
+ const last = logs.value.at(-1)?.date
+ const hasTimeRange = Boolean(
+ first &&
+ last &&
+ Number.isFinite(Date.parse(first)) &&
+ Number.isFinite(Date.parse(last)),
+ )
+ return { errors, warnings, first, last, hasTimeRange }
+})
+
const availableTags = computed(() => {
const tags = new Set<string>()
for (const log of logs.value) {
@@ -194,6 +189,24 @@ const availableTags = computed(() => {
}))
})
+const tagMeasureContext = shallowRef<CanvasRenderingContext2D | null>(null)
+const timeColumnWidth = computed(() => {
+ const width = tagMeasureContext.value?.measureText('23:59:59.999').width ?? 96
+ return `${Math.ceil(width) + 16}px`
+})
+const tagColumnWidth = computed(() => {
+ const context = tagMeasureContext.value
+ let width = 0
+ for (const tag of availableTags.value) {
+ // The fallback also reserves enough room for full-width characters.
+ const measured = context
+ ? context.measureText(tag.value).width
+ : Array.from(tag.value).length * 12
+ width = Math.max(width, measured)
+ }
+ return `${Math.ceil(width) + 16}px`
+})
+
const totalVirtualHeight = computed(
() => filteredLogs.value.length * ROW_HEIGHT + VIRTUAL_BOTTOM_PADDING,
)
@@ -267,7 +280,10 @@ const getLogLineStyle = (log: LogEntry) => {
const getConnectionStatusClass = () => {
if (connectionState.value === 'live') {
return 'text-success'
- } else if (connectionState.value === 'connecting' || connectionState.value === 'loadingHistory') {
+ } else if (
+ connectionState.value === 'connecting' ||
+ connectionState.value === 'loadingHistory'
+ ) {
return 'text-info'
} else if (connectionState.value === 'error') {
return 'text-error'
@@ -286,7 +302,10 @@ const getConnectionStatusColor = ():
if (connectionState.value === 'live') {
return 'success'
}
- if (connectionState.value === 'connecting' || connectionState.value === 'loadingHistory') {
+ if (
+ connectionState.value === 'connecting' ||
+ connectionState.value === 'loadingHistory'
+ ) {
return 'info'
}
if (connectionState.value === 'error') {
@@ -314,15 +333,21 @@ const highlightSearch = (content: string) => {
const toggleAutoScroll = () => {
autoScroll.value = !autoScroll.value
if (autoScroll.value) {
- scrollToBottom()
+ void scrollToBottom('smooth')
}
}
// 滚动到底部
-const scrollToBottom = async () => {
+const scrollToBottom = async (behavior: ScrollBehavior = 'instant') => {
await nextTick()
- if (logContainer.value) {
- logContainer.value.scrollTop = logContainer.value.scrollHeight
+ if (logContainer.value && autoScroll.value && !disposed) {
+ const top = Math.max(
+ 0,
+ logContainer.value.scrollHeight - logContainer.value.clientHeight,
+ )
+ smoothScrollTarget = behavior === 'smooth' ? top : null
+ if (behavior !== 'smooth') scrollTop.value = top
+ logContainer.value.scrollTo({ top, behavior })
}
}
@@ -335,8 +360,21 @@ const handleScroll = () => {
scrollHeight,
clientHeight,
} = logContainer.value
+ const previousTop = scrollTop.value
scrollTop.value = currentScrollTop
containerHeight.value = clientHeight
+ // Animation frames are programmatic scrolling, not upward history navigation.
+ if (smoothScrollTarget !== null) {
+ if (Math.abs(currentScrollTop - smoothScrollTarget) <= 1)
+ smoothScrollTarget = null
+ else return
+ }
+ if (
+ isInitialLoading.value ||
+ isPositioningInitialLogs ||
+ isLoadingOlder.value
+ )
+ return
const isNearBottom = currentScrollTop + clientHeight >= scrollHeight - 50 // 距离底部50px以内
const isAtTop = currentScrollTop + clientHeight < scrollHeight - 200 // 距离底部200px以上
@@ -348,71 +386,150 @@ const handleScroll = () => {
else if (isAtTop && autoScroll.value) {
autoScroll.value = false
}
-}
-// 连接日志流
-const connectLogStream = () => {
- if (eventSource) {
- eventSource.close()
+ if (
+ currentScrollTop < previousTop &&
+ currentScrollTop <= HISTORY_LOAD_THRESHOLD &&
+ !autoScroll.value
+ ) {
+ void loadOlderLogs()
}
+}
- // 重置状态
- logs.value = []
- batchQueue.value = []
- isInitialLoading.value = true
- loadingProgress.value = 5
+// History and live delivery share byte cursors, so writes during the fetch are retained.
+const appendLines = (lines: string[]) => {
+ const entries = lines
+ .map(parseLogLine)
+ .filter((entry): entry is LogEntry => entry !== null)
+ logs.value = [...logs.value, ...entries].slice(-MAX_LOG_LINES)
+ if (autoScroll.value) void scrollToBottom('smooth')
+}
+const openLiveStream = () => {
+ if (disposed) return
connectionState.value = 'connecting'
- eventSource = new EventSource(`/api/system/logs?initial=${INITIAL_LOG_LINES}`)
-
- let initialLoadCompleteTimer: NodeJS.Timeout | null = null
- const MESSAGE_TIMEOUT = 2000 // 消息间隔超时时间(毫秒)
-
- eventSource.onopen = () => {
+ eventSource?.close()
+ const source = new EventSource(
+ `/api/system/logs?stream=1&offset=${liveOffset}&identity=${encodeURIComponent(fileIdentity)}`,
+ )
+ eventSource = source
+ source.onopen = () => {
+ if (source !== eventSource || disposed) return
isConnected.value = true
- connectionState.value = 'loadingHistory'
- }
-
- eventSource.onmessage = (event) => {
- const logLine = event.data
- if (logLine && logLine.trim()) {
- const logEntry = parseLogLine(logLine)
- if (logEntry) {
- // 清除之前的定时器
- if (initialLoadCompleteTimer) {
- clearTimeout(initialLoadCompleteTimer)
- initialLoadCompleteTimer = null
- }
-
- if (isInitialLoading.value) {
- addLogEntry(logEntry)
- loadingProgress.value = Math.min(90, loadingProgress.value + 0.2)
-
- // 设置新的定时器,如果在指定时间内没有新消息,认为初始加载完成
- initialLoadCompleteTimer = setTimeout(() => {
- if (isInitialLoading.value) {
- connectionState.value = 'live'
- // 让加载指示器显示完成状态后再隐藏
- setTimeout(() => {
- isInitialLoading.value = false
- loadingProgress.value = 100
- autoScroll.value = true
- scrollToBottom()
- }, 500)
- }
- }, MESSAGE_TIMEOUT)
- } else {
- // 实时日志直接添加
- addLogEntry(logEntry)
- }
- }
- }
+ connectionState.value = 'live'
}
-
- eventSource.onerror = (error) => {
+ source.addEventListener('logs', (event) => {
+ if (source !== eventSource || disposed) return
+ const batch = JSON.parse((event as MessageEvent).data) as LogPage
+ liveOffset = batch.offset
+ fileIdentity = batch.identity
+ appendLines(batch.lines)
+ })
+ source.addEventListener('reset', () => {
+ if (source !== eventSource || disposed) return
+ void connectLogStream()
+ })
+ const retry = () => {
+ if (source !== eventSource || disposed) return
+ source.close()
+ if (reconnectTimer) clearTimeout(reconnectTimer)
isConnected.value = false
connectionState.value = 'error'
- console.error('EventSource error:', error)
+ reconnectTimer = setTimeout(openLiveStream, 2000)
+ }
+ source.addEventListener('failure', retry)
+ source.onerror = retry
+}
+
+const connectLogStream = async () => {
+ eventSource?.close()
+ eventSource = null
+ if (reconnectTimer) clearTimeout(reconnectTimer)
+ requestController?.abort()
+ const controller = new AbortController()
+ requestController = controller
+ isInitialLoading.value = true
+ isPositioningInitialLogs = true
+ isConnected.value = false
+ connectionState.value = 'loadingHistory'
+ try {
+ const page = await $fetch<LogPage>('/api/system/logs', {
+ signal: controller.signal,
+ })
+ if (controller.signal.aborted || disposed) return
+ autoScroll.value = true
+ logs.value = page.lines
+ .map(parseLogLine)
+ .filter((entry): entry is LogEntry => entry !== null)
+ historyCursor.value = page.before
+ liveOffset = page.offset
+ fileIdentity = page.identity
+ // Remove the loading hint before measuring the final viewport height.
+ isInitialLoading.value = false
+ await scrollToBottom()
+ if (controller.signal.aborted || disposed) return
+ openLiveStream()
+ } catch {
+ if (!controller.signal.aborted && !disposed) connectionState.value = 'error'
+ } finally {
+ if (requestController === controller) {
+ isInitialLoading.value = false
+ isPositioningInitialLogs = false
+ }
+ }
+}
+
+const loadOlderLogs = async () => {
+ if (
+ isInitialLoading.value ||
+ isLoadingOlder.value ||
+ !historyCursor.value ||
+ logs.value.length >= MAX_LOG_LINES
+ )
+ return
+ const controller = requestController
+ let loaded = false
+ isLoadingOlder.value = true
+ autoScroll.value = false
+ try {
+ const page = await $fetch<LogPage>('/api/system/logs', {
+ query: { before: historyCursor.value, identity: fileIdentity },
+ signal: controller?.signal,
+ })
+ if (controller !== requestController || disposed) return
+ const previousTop = logContainer.value?.scrollTop ?? 0
+ const previousVisibleCount = filteredLogs.value.length
+ const entries = page.lines
+ .map(parseLogLine)
+ .filter((entry): entry is LogEntry => entry !== null)
+ const capacity = Math.max(0, MAX_LOG_LINES - logs.value.length)
+ const added = capacity > 0 ? entries.slice(-capacity) : []
+ logs.value = [...added, ...logs.value]
+ historyCursor.value = page.before
+ // Update the virtual range in the same render as the prepend. Count only rows
+ // that pass the current filters so the visible log retains its pixel position.
+ const restoredTop =
+ previousTop +
+ (filteredLogs.value.length - previousVisibleCount) * ROW_HEIGHT
+ scrollTop.value = restoredTop
+ await nextTick()
+ if (controller !== requestController || disposed) return
+ logContainer.value?.scrollTo({ top: restoredTop, behavior: 'instant' })
+ loaded = true
+ } catch {
+ if (!controller?.signal.aborted && !disposed)
+ connectionState.value = 'error'
+ } finally {
+ isLoadingOlder.value = false
+ }
+ // Filtered or short pages may leave the viewport near the top without a new
+ // scroll event. Continue prefetching until there is enough visible history.
+ if (
+ loaded &&
+ !autoScroll.value &&
+ (logContainer.value?.scrollTop ?? Infinity) <= HISTORY_LOAD_THRESHOLD
+ ) {
+ void loadOlderLogs()
}
}
@@ -428,6 +545,13 @@ watch(
onMounted(() => {
if (logContainer.value) {
+ const context = document.createElement('canvas').getContext('2d')
+ if (context) {
+ const fontSize =
+ parseFloat(getComputedStyle(document.documentElement).fontSize) * 0.75
+ context.font = `${fontSize}px ${getComputedStyle(logContainer.value).fontFamily}`
+ tagMeasureContext.value = context
+ }
containerHeight.value = logContainer.value.clientHeight
}
@@ -444,6 +568,9 @@ onMounted(() => {
})
onUnmounted(() => {
+ disposed = true
+ requestController?.abort()
+ if (reconnectTimer) clearTimeout(reconnectTimer)
if (eventSource) {
eventSource.close()
}
@@ -489,10 +616,32 @@ onUnmounted(() => {
</UBadge>
</div>
<div
- class="mt-1 flex items-center gap-2 text-xs text-neutral-500 dark:text-neutral-400"
+ class="mt-1 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-neutral-500 dark:text-neutral-400"
>
- <span>{{ $t('dashboard.logs.countLabel', { total: logs.length, shown: filteredLogs.length }) }}</span>
- <span v-if="availableTags.length">{{ $t('dashboard.logs.tagCount', { count: availableTags.length }) }}</span>
+ <span>{{
+ $t('dashboard.logs.loadedCount', { count: logs.length })
+ }}</span>
+ <span v-if="hasActiveFilters">{{
+ $t('dashboard.logs.matchedCount', {
+ count: filteredLogs.length,
+ })
+ }}</span>
+ <span :class="{ 'text-error': logSummary.errors > 0 }">{{
+ $t('dashboard.logs.errorCount', { count: logSummary.errors })
+ }}</span>
+ <span :class="{ 'text-warning': logSummary.warnings > 0 }">{{
+ $t('dashboard.logs.warningCount', {
+ count: logSummary.warnings,
+ })
+ }}</span>
+ <span v-if="logSummary.hasTimeRange">{{
+ $t('dashboard.logs.timeRange', {
+ start: $dayjs(logSummary.first).format(
+ 'YYYY-MM-DD HH:mm:ss',
+ ),
+ end: $dayjs(logSummary.last).format('YYYY-MM-DD HH:mm:ss'),
+ })
+ }}</span>
</div>
</div>
<span
@@ -563,78 +712,80 @@ onUnmounted(() => {
class="ms-auto sm:ms-0"
@click="toggleAutoScroll"
/>
- <!-- Download raw log -->
- <!-- TODO: Download raw log file -->
- <!-- <UButton
- as="a"
- target="_blank"
- rel="noopener"
- icon="tabler:download"
- color="neutral"
- size="sm"
- variant="soft"
- /> -->
+ <UButton
+ v-if="connectionState === 'error'"
+ icon="tabler:refresh"
+ color="neutral"
+ size="sm"
+ @click="connectLogStream"
+ />
</div>
</div>
- <!-- 加载进度指示器 -->
<div
v-if="isInitialLoading"
- class="absolute inset-0 bg-white/80 dark:bg-black/80 backdrop-blur-sm flex items-center justify-center z-10"
+ class="px-4 py-2 text-sm text-neutral-500"
+ role="status"
>
- <div class="text-center">
- <div class="mb-4">
- <UIcon
- name="tabler:loader-2"
- class="animate-spin w-8 h-8 text-blue-500"
- />
- </div>
- <div class="text-sm text-gray-600 dark:text-gray-400 mb-2">
- {{ $t('dashboard.logs.connectionStatus.loadingHistory') }}
- </div>
- <div class="w-64 bg-gray-200 dark:bg-gray-700 rounded-full h-2">
- <div
- class="bg-blue-500 h-2 rounded-full transition-all duration-300 ease-out"
- :style="{ width: `${loadingProgress}%` }"
- ></div>
- </div>
- <div class="text-xs text-gray-500 dark:text-gray-400 mt-1">
- {{ Math.round(loadingProgress) }}%
- </div>
- </div>
+ <UIcon
+ name="tabler:loader-2"
+ class="animate-spin mr-2"
+ />
+ {{ $t('dashboard.logs.connectionStatus.loadingHistory') }}
</div>
<div
ref="logContainer"
- class="flex-1 min-h-0 overflow-y-auto overflow-x-auto scroll-smooth font-mono text-sm relative"
+ class="flex-1 min-h-0 min-w-0 overflow-y-auto overflow-x-auto font-mono text-sm relative"
+ style="overflow-anchor: none"
+ :style="{
+ '--log-tag-width': tagColumnWidth,
+ '--log-time-width': timeColumnWidth,
+ }"
@scroll="handleScroll"
+ @wheel.passive="cancelSmoothScroll"
+ @touchstart.passive="cancelSmoothScroll"
+ @pointerdown="cancelSmoothScroll"
+ @keydown="cancelSmoothScroll"
>
+ <div
+ v-if="isLoadingOlder"
+ class="sticky top-0 h-0 z-10 flex justify-center pointer-events-none"
+ role="status"
+ >
+ <UIcon
+ name="tabler:loader-2"
+ class="animate-spin mt-2 text-blue-500"
+ :aria-label="$t('dashboard.logs.connectionStatus.loadingHistory')"
+ />
+ </div>
<div
class="relative"
:style="{ height: `${totalVirtualHeight}px` }"
>
<div
- class="absolute left-0 right-0 px-2"
+ class="absolute left-0 w-max min-w-full"
:style="{ transform: `translateY(${virtualOffset}px)` }"
>
<div
v-for="(log, index) in visibleLogs"
:key="`${virtualStart + index}-${log.raw}`"
:class="[
- 'px-2 py-0.5 rounded border-l-4',
+ 'group log-row grid items-center',
getLogLineStyle(log),
]"
:style="{ height: `${ROW_HEIGHT}px` }"
>
- <div class="flex items-center space-x-3 text-sm h-full">
- <!-- 时间戳 -->
- <span
- class="text-neutral-400 dark:text-neutral-500 text-xs whitespace-nowrap min-w-0 shrink-0"
- >
- {{ $dayjs(log.date).format('HH:mm:ss.SSS') }}
- </span>
+ <!-- 时间戳 -->
+ <span
+ class="log-time log-fixed-cell text-neutral-400 dark:text-neutral-500 text-xs whitespace-nowrap"
+ >
+ {{ $dayjs(log.date).format('HH:mm:ss.SSS') }}
+ </span>
- <!-- 日志级别 -->
+ <!-- 日志级别 -->
+ <div class="log-level log-fixed-cell">
<UBadge
+ class="shrink-0"
size="sm"
:variant="log.level <= 1 ? 'solid' : 'soft'"
:color="getBadgeColor(log.type || getLevelType(log.level))"
@@ -645,30 +796,30 @@ onUnmounted(() => {
.slice(0, 4)
}}
</UBadge>
+ </div>
- <!-- 日志内容 -->
- <div class="flex-1 min-w-0">
- <span
- v-if="normalizedSearchQuery"
- class="block whitespace-nowrap overflow-hidden text-ellipsis"
- v-html="highlightSearch(log.message)"
- ></span>
- <span
- v-else
- class="block whitespace-nowrap overflow-hidden text-ellipsis"
- >
- {{ log.message }}
- </span>
- </div>
-
- <!-- 标签 -->
+ <!-- 日志内容 -->
+ <div class="px-2 min-w-0">
<span
- v-if="log.tag"
- class="text-xs whitespace-nowrap shrink-0 truncate text-neutral-400/80"
+ v-if="normalizedSearchQuery"
+ class="block whitespace-nowrap"
+ v-html="highlightSearch(log.message)"
+ ></span>
+ <span
+ v-else
+ class="block whitespace-nowrap"
>
- {{ log.tag }}
+ {{ log.message }}
</span>
</div>
+
+ <!-- 标签 -->
+ <span
+ class="log-tag log-fixed-cell text-xs whitespace-nowrap text-neutral-400/80"
+ :title="log.tag"
+ >
+ {{ log.tag }}
+ </span>
</div>
</div>
@@ -677,7 +828,9 @@ onUnmounted(() => {
v-if="filteredLogs.length === 0"
class="text-center py-8 text-gray-500 dark:text-gray-400 absolute inset-0"
>
- <div v-if="logs.length === 0">{{ $t('dashboard.logs.empty.waiting') }}</div>
+ <div v-if="logs.length === 0">
+ {{ $t('dashboard.logs.empty.waiting') }}
+ </div>
<div v-else>{{ $t('dashboard.logs.empty.noMatch') }}</div>
</div>
</div>
@@ -688,9 +841,66 @@ onUnmounted(() => {
</template>
<style scoped>
+/* The shared horizontal scroll moves messages while metadata stays pinned. */
+.log-row {
+ --log-level-width: 3.5rem;
+ grid-template-columns:
+ var(--log-time-width) var(--log-level-width) minmax(6rem, 1fr)
+ var(--log-tag-width);
+}
+
+.log-fixed-cell {
+ position: sticky;
+ z-index: 1;
+ height: 100%;
+ align-content: center;
+ padding-inline: 0.5rem;
+ background: var(--color-neutral-100);
+}
+
+:global(.dark) .log-fixed-cell {
+ background: var(--color-neutral-950);
+}
+
+.log-row:hover .log-fixed-cell {
+ background: var(--color-neutral-200);
+}
+
+:global(.dark) .log-row:hover .log-fixed-cell {
+ background: var(--color-neutral-800);
+}
+
+.log-time {
+ left: 0;
+}
+.log-level {
+ left: var(--log-time-width);
+}
+.log-tag {
+ right: 0;
+ text-align: right;
+}
+
+@media (width < 640px) {
+ .log-row {
+ --log-level-width: 3rem;
+ }
+ .log-fixed-cell {
+ padding-inline: 0.25rem;
+ }
+ .log-level,
+ .log-tag {
+ position: static;
+ left: auto;
+ right: auto;
+ z-index: auto;
+ }
+}
+
/* 自定义滚动条样式 */
.overflow-y-auto::-webkit-scrollbar {
width: 8px;
+ height: 8px;
}
.overflow-y-auto::-webkit-scrollbar-track {
diff --git a/i18n/locales/en.json b/i18n/locales/en.json
index 8124a42a..fe284342 100644
--- a/i18n/locales/en.json
+++ b/i18n/locales/en.json
@@ -1107,8 +1107,11 @@
"levelPlaceholder": "Filter level",
"tagPlaceholder": "Filter Tag"
},
- "countLabel": "Total/Shown: {total}/{shown}",
- "tagCount": "Tags: {count}",
+ "loadedCount": "{count} logs loaded",
+ "matchedCount": "{count} matches",
+ "errorCount": "Errors {count}",
+ "warningCount": "Warnings {count}",
+ "timeRange": "Time range: {start} – {end}",
"empty": {
"waiting": "Waiting for log data...",
"noMatch": "No matching log entries"
diff --git a/i18n/locales/ja.json b/i18n/locales/ja.json
index 0513358f..7263ba15 100644
--- a/i18n/locales/ja.json
+++ b/i18n/locales/ja.json
@@ -1033,8 +1033,11 @@
"levelPlaceholder": "レベルで絞り込む",
"tagPlaceholder": "タグで絞り込む"
},
- "countLabel": "合計/表示: {total}/{shown}",
- "tagCount": "タグ: {count}",
+ "loadedCount": "読み込み済み {count} 件",
+ "matchedCount": "一致 {count} 件",
+ "errorCount": "エラー {count}",
+ "warningCount": "警告 {count}",
+ "timeRange": "期間:{start} ~ {end}",
"empty": {
"waiting": "ログデータを待機中...",
"noMatch": "一致するログエントリがありません"
diff --git a/i18n/locales/ru.json b/i18n/locales/ru.json
index 2d4452db..8e6b4b4d 100644
--- a/i18n/locales/ru.json
+++ b/i18n/locales/ru.json
@@ -1106,8 +1106,11 @@
"levelPlaceholder": "Уровень журнала",
"tagPlaceholder": "Фильтр по тегу"
},
- "countLabel": "Всего / показано: {total} / {shown}",
- "tagCount": "Тегов: {count}",
+ "loadedCount": "Загружено: {count}",
+ "matchedCount": "Совпадений: {count}",
+ "errorCount": "Ошибок: {count}",
+ "warningCount": "Предупреждений: {count}",
+ "timeRange": "Период: {start} – {end}",
"empty": {
"waiting": "Ожидание записей журнала…",
"noMatch": "Подходящие записи не найдены"
diff --git a/i18n/locales/zh-Hans.json b/i18n/locales/zh-Hans.json
index 75cb98a1..bc13de80 100644
--- a/i18n/locales/zh-Hans.json
+++ b/i18n/locales/zh-Hans.json
@@ -1107,8 +1107,11 @@
"levelPlaceholder": "过滤级别",
"tagPlaceholder": "筛选 Tag"
},
- "countLabel": "总条数/展示: {total}/{shown}",
- "tagCount": "Tag: {count}",
+ "loadedCount": "已载入 {count} 条",
+ "matchedCount": "匹配 {count} 条",
+ "errorCount": "错误 {count}",
+ "warningCount": "警告 {count}",
+ "timeRange": "时间范围:{start} ~ {end}",
"empty": {
"waiting": "等待日志数据...",
"noMatch": "没有匹配的日志条目"
diff --git a/i18n/locales/zh-Hant-HK.json b/i18n/locales/zh-Hant-HK.json
index 1c8e7348..c0a94d37 100644
--- a/i18n/locales/zh-Hant-HK.json
+++ b/i18n/locales/zh-Hant-HK.json
@@ -1033,8 +1033,11 @@
"levelPlaceholder": "篩選等級",
"tagPlaceholder": "篩選 Tag"
},
- "countLabel": "總筆數/顯示: {total}/{shown}",
- "tagCount": "Tag: {count}",
+ "loadedCount": "已載入 {count} 筆",
+ "matchedCount": "符合 {count} 筆",
+ "errorCount": "錯誤 {count}",
+ "warningCount": "警告 {count}",
+ "timeRange": "時間範圍:{start} ~ {end}",
"empty": {
"waiting": "等待日誌資料...",
"noMatch": "沒有符合的日誌條目"
diff --git a/i18n/locales/zh-Hant-TW.json b/i18n/locales/zh-Hant-TW.json
index d83ce29a..6d4195eb 100644
--- a/i18n/locales/zh-Hant-TW.json
+++ b/i18n/locales/zh-Hant-TW.json
@@ -1033,8 +1033,11 @@
"levelPlaceholder": "篩選等級",
"tagPlaceholder": "篩選 Tag"
},
- "countLabel": "總筆數/顯示: {total}/{shown}",
- "tagCount": "Tag: {count}",
+ "loadedCount": "已載入 {count} 筆",
+ "matchedCount": "符合 {count} 筆",
+ "errorCount": "錯誤 {count}",
+ "warningCount": "警告 {count}",
+ "timeRange": "時間範圍:{start} ~ {end}",
"empty": {
"waiting": "等待日誌資料...",
"noMatch": "沒有符合的日誌條目"
diff --git a/server/api/system/logs.ts b/server/api/system/logs.ts
index 2da7e0c6..47918315 100644
--- a/server/api/system/logs.ts
+++ b/server/api/system/logs.ts
@@ -1,216 +1,120 @@
-import fs from 'node:fs'
+import { open } from 'node:fs/promises'
import path from 'node:path'
-
-const DEFAULT_INITIAL_LINES = 400
-const MAX_INITIAL_LINES = 2000
-const ALL_INITIAL_LINES = 'all'
-
-type InitialLinesMode = number | typeof ALL_INITIAL_LINES
-
-const clampInitialLines = (value: unknown): InitialLinesMode => {
- if (typeof value === 'string' && value.toLowerCase() === ALL_INITIAL_LINES) {
- return ALL_INITIAL_LINES
- }
-
- const parsed = Number(value)
- if (!Number.isFinite(parsed)) {
- return DEFAULT_INITIAL_LINES
- }
- return Math.max(0, Math.min(MAX_INITIAL_LINES, Math.floor(parsed)))
-}
-
-const readLastLines = async (
- filePath: string,
- maxLines: number,
-): Promise<{ lines: string[]; fileSize: number }> => {
- const handle = await fs.promises.open(filePath, 'r')
- try {
- const stat = await handle.stat()
- const fileSize = stat.size
-
- if (maxLines <= 0 || fileSize <= 0) {
- return { lines: [], fileSize }
- }
-
- const chunkSize = 64 * 1024
- const maxReadBytes = 2 * 1024 * 1024
- let position = fileSize
- let totalReadBytes = 0
- let content = ''
- let newlineCount = 0
-
- while (position > 0 && newlineCount <= maxLines && totalReadBytes < maxReadBytes) {
- const readSize = Math.min(chunkSize, position)
- const start = position - readSize
- const chunk = Buffer.allocUnsafe(readSize)
- const { bytesRead } = await handle.read(chunk, 0, readSize, start)
- if (bytesRead <= 0) {
- break
- }
-
- content = chunk.toString('utf-8', 0, bytesRead) + content
- position = start
- totalReadBytes += bytesRead
- newlineCount = (content.match(/\n/g) || []).length
- }
-
- const lines = content
- .split('\n')
- .map((line) => line.trim())
- .filter(Boolean)
- .slice(-maxLines)
-
- return { lines, fileSize }
- } finally {
- await handle.close()
- }
-}
-
-const readAllLines = async (
- filePath: string,
-): Promise<{ lines: string[]; fileSize: number }> => {
- const stat = await fs.promises.stat(filePath)
- if (stat.size <= 0) {
- return { lines: [], fileSize: stat.size }
- }
-
- const content = await fs.promises.readFile(filePath, 'utf-8')
- const lines = content
- .split('\n')
- .map((line) => line.trim())
- .filter(Boolean)
-
- return { lines, fileSize: stat.size }
-}
-
-const streamNewLines = async (
- filePath: string,
- fromOffset: number,
-): Promise<{ lines: string[]; nextOffset: number }> => {
- const stat = await fs.promises.stat(filePath)
- const safeOffset = Math.max(0, Math.min(fromOffset, stat.size))
- const nextOffset = stat.size
-
- if (nextOffset <= safeOffset) {
- return { lines: [], nextOffset }
- }
-
- const stream = fs.createReadStream(filePath, {
- encoding: 'utf-8',
- start: safeOffset,
- end: nextOffset - 1,
- })
-
- let buffer = ''
- for await (const chunk of stream) {
- buffer += chunk
- }
-
- const lines = buffer
- .split('\n')
- .map((line) => line.trim())
- .filter(Boolean)
-
- return { lines, nextOffset }
-}
+import {
+ LOG_PAGE_BYTES,
+ logIdentity,
+ readLogPage,
+} from '../../utils/log-reader'
export default defineEventHandler(async (event) => {
await requireUserSession(event)
-
- const eventStream = createEventStream(event)
-
- const logFilePath = path.join(process.cwd(), 'data', 'logs', 'app.log')
- const logDirPath = path.dirname(logFilePath)
+ const file = path.join(process.cwd(), 'data', 'logs', 'app.log')
const query = getQuery(event)
- const initialLines = clampInitialLines(query.initial)
-
- fs.mkdirSync(logDirPath, { recursive: true })
-
- let lastReadOffset = 0
- let isClosed = false
- let flushScheduled = false
- let isFlushing = false
-
- setImmediate(async () => {
+ const parseOffset = (value: unknown) => {
+ if (value === undefined) return undefined
+ const offset = Number(value)
+ if (!Number.isSafeInteger(offset) || offset < 0) {
+ throw createError({
+ statusCode: 400,
+ statusMessage: 'Invalid log cursor',
+ })
+ }
+ return offset
+ }
+ const cursor = parseOffset(query.before)
+ if (query.stream !== '1') {
try {
- if (fs.existsSync(logFilePath)) {
- const { lines, fileSize } =
- initialLines === ALL_INITIAL_LINES
- ? await readAllLines(logFilePath)
- : await readLastLines(logFilePath, initialLines)
- for (const line of lines) {
- if (isClosed) {
- return
- }
- await eventStream.push(line)
- }
- lastReadOffset = fileSize
+ const page = await readLogPage(file, cursor)
+ if (query.identity && query.identity !== page.identity) {
+ throw createError({
+ statusCode: 409,
+ statusMessage: 'Log file changed; reload history',
+ })
}
+ return page
} catch (error) {
- console.error('Error initializing log stream:', error)
- }
- })
-
- const flushNewLogs = async () => {
- if (isClosed || isFlushing) {
- return
+ if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
+ return { lines: [], before: 0, offset: 0, identity: '' }
}
+ }
- isFlushing = true
+ const stream = createEventStream(event)
+ let offset = parseOffset(query.offset) ?? 0
+ let identity = String(query.identity || '')
+ let closed = false
+ let busy = false
+ let ticks = 0
+ let skippingOversized = false
+ const flush = async () => {
+ if (closed || busy) return
+ busy = true
+ let handle
try {
- if (!fs.existsSync(logFilePath)) {
- lastReadOffset = 0
- return
+ handle = await open(file, 'r')
+ if (closed) return
+ const stat = await handle.stat()
+ const currentIdentity = logIdentity(stat)
+ if ((identity && currentIdentity !== identity) || stat.size < offset) {
+ offset = 0
+ skippingOversized = false
+ await stream.push({ event: 'reset', data: '{}' })
}
-
- const stat = await fs.promises.stat(logFilePath)
- if (stat.size < lastReadOffset) {
- // 日志被截断或轮转,重置偏移
- lastReadOffset = 0
- }
-
- const { lines, nextOffset } = await streamNewLines(logFilePath, lastReadOffset)
- for (const line of lines) {
- if (isClosed) {
- return
+ identity = currentIdentity
+ if (stat.size > offset) {
+ const buffer = Buffer.alloc(
+ Math.min(LOG_PAGE_BYTES, stat.size - offset),
+ )
+ const { bytesRead } = await handle.read(
+ buffer,
+ 0,
+ buffer.length,
+ offset,
+ )
+ const data = buffer.subarray(0, bytesRead)
+ const first = skippingOversized ? data.indexOf(10) + 1 : 0
+ const last = data.lastIndexOf(10)
+ if (last >= 0) {
+ offset += last + 1
+ skippingOversized = false
+ await stream.push({
+ event: 'logs',
+ id: String(offset),
+ data: JSON.stringify({
+ lines: data
+ .subarray(first, last)
+ .toString('utf8')
+ .split('\n')
+ .filter(Boolean),
+ offset,
+ identity,
+ }),
+ })
+ } else if (bytesRead === LOG_PAGE_BYTES) {
+ // Skip oversized records in bounded chunks instead of growing memory without limit.
+ offset += bytesRead
+ skippingOversized = true
}
- await eventStream.push(line)
}
- lastReadOffset = nextOffset
+ if (++ticks % 60 === 0)
+ await stream.push({ event: 'heartbeat', data: '{}' })
} catch (error) {
- console.error('Error flushing log stream:', error)
- } finally {
- isFlushing = false
- if (flushScheduled) {
- flushScheduled = false
- setImmediate(() => {
- void flushNewLogs()
- })
+ if (!closed && (error as NodeJS.ErrnoException).code !== 'ENOENT') {
+ await stream.push({ event: 'failure', data: '{}' }).catch(() => {})
}
+ } finally {
+ await handle?.close()
+ busy = false
}
}
-
- const scheduleFlush = () => {
- flushScheduled = true
- setImmediate(() => {
- void flushNewLogs()
- })
- }
-
- const watcher = fs.watch(logDirPath, (_eventType, filename) => {
- if (!filename) {
- return
- }
- if (filename.toString() === path.basename(logFilePath)) {
- scheduleFlush()
- }
+ const timer = setInterval(() => {
+ void flush()
+ }, 250)
+ stream.onClosed(() => {
+ closed = true
+ clearInterval(timer)
})
-
- eventStream.onClosed(async () => {
- isClosed = true
- watcher.close()
- await eventStream.close()
- })
-
- return eventStream.send()
+ // Flush an initial event so quiet logs do not delay the browser's open event.
+ void stream.push({ event: 'ready', data: '{}' }).catch(() => {})
+ void flush()
+ return stream.send()
})
diff --git a/server/utils/log-reader.ts b/server/utils/log-reader.ts
new file mode 100644
index 00000000..c091d07c
--- /dev/null
+++ b/server/utils/log-reader.ts
@@ -0,0 +1,37 @@
+import { open } from 'node:fs/promises'
+
+// Bound work by bytes, regardless of the size of the log archive.
+export const LOG_PAGE_BYTES = 64 * 1024
+export const logIdentity = (stat: { dev: number; ino: number }) =>
+ `${stat.dev}:${stat.ino}`
+
+export async function readLogPage(file: string, before?: number) {
+ const handle = await open(file, 'r')
+ try {
+ const stat = await handle.stat()
+ const end = Math.min(before ?? stat.size, stat.size)
+ const start = Math.max(0, end - LOG_PAGE_BYTES)
+ const buffer = Buffer.alloc(end - start)
+ const { bytesRead } = await handle.read(buffer, 0, buffer.length, start)
+ const data = buffer.subarray(0, bytesRead)
+ // Only return complete records; an unfinished tail belongs to the live stream.
+ const first = start === 0 ? 0 : data.indexOf(10) + 1
+ const last = data.lastIndexOf(10)
+ const boundary = last < first ? start : start + last + 1
+ return {
+ lines:
+ last >= first
+ ? data
+ .subarray(first, last)
+ .toString('utf8')
+ .split('\n')
+ .filter(Boolean)
+ : [],
+ before: start === 0 ? 0 : start + first,
+ offset: boundary,
+ identity: logIdentity(stat),
+ }
+ } finally {
+ await handle.close()
+ }
+}