diff --git a/packages/core/src/webui/ui-config.ts b/packages/core/src/webui/ui-config.ts index 0484c54d..efd2404f 100644 --- a/packages/core/src/webui/ui-config.ts +++ b/packages/core/src/webui/ui-config.ts @@ -158,6 +158,8 @@ export interface UiHighlightRule { color: string; } +export type LogsPreset = 'dev' | 'ops' | 'minimal' | 'custom'; + export interface UiLogsPrefs { /** Log levels shown by default (client display filter). */ visibleLevels: string[]; @@ -167,6 +169,9 @@ export interface UiLogsPrefs { wrap: boolean; /** Keyword → colour row highlights. */ highlightRules: UiHighlightRule[]; + /** Active view preset id; 'custom' = the operator hand-tuned the prefs. The + * client owns the preset → prefs bundles; the server just stores the id. */ + preset: LogsPreset; } export interface UiPages { @@ -278,6 +283,7 @@ const DEFAULT_PAGES: UiPages = { autoScroll: true, wrap: true, highlightRules: [], + preset: 'custom', }, processesSort: 'pid', configTab: '', @@ -571,6 +577,7 @@ export function normalizePages(value: unknown): UiPages { autoScroll: boolOr(logs.autoScroll, DEFAULT_PAGES.logs.autoScroll), wrap: boolOr(logs.wrap, DEFAULT_PAGES.logs.wrap), highlightRules: normalizeHighlightRules(logs.highlightRules), + preset: oneOf(logs.preset, ['dev', 'ops', 'minimal', 'custom'], 'custom'), }, processesSort: idOr(v.processesSort, DEFAULT_PAGES.processesSort), configTab: typeof v.configTab === 'string' ? v.configTab.slice(0, 64) : DEFAULT_PAGES.configTab, diff --git a/packages/webui/src/components/pages/logs-page.tsx b/packages/webui/src/components/pages/logs-page.tsx index 7d29c4e5..3978eccf 100644 --- a/packages/webui/src/components/pages/logs-page.tsx +++ b/packages/webui/src/components/pages/logs-page.tsx @@ -1,6 +1,6 @@ import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from 'react'; import { motion, AnimatePresence } from 'motion/react'; -import { ArrowDownToLine, Filter, Highlighter, Inbox, Pause, Plus, RefreshCw, Search, SearchX, SlidersHorizontal, Trash2, WrapText, X } from 'lucide-react'; +import { ArrowDownToLine, Download, Filter, Highlighter, Inbox, Pause, Plus, RefreshCw, Search, SearchX, SlidersHorizontal, Trash2, WrapText, X } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; @@ -8,7 +8,7 @@ import { ScrollArea } from '@/components/ui/scroll-area'; import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'; import { ConfirmDialog } from '@/components/confirm-dialog'; import { cn } from '@/lib/utils'; -import type { LogEntry, LogLevel, UiHighlightRule } from '@/types'; +import type { LogEntry, LogLevel, LogsPreset, UiHighlightRule } from '@/types'; import { useApi } from '@/lib/api'; import { useTheme } from '@/contexts/ThemeContext'; import { useLayout } from '@/contexts/LayoutContext'; @@ -24,6 +24,15 @@ const levelClass: Record = { const LEVELS: LogLevel[] = ['trace', 'debug', 'info', 'success', 'warn', 'error']; +// View presets bundle the display prefs (levels/maxLines/autoScroll/wrap). The +// server stores only the id; 'custom' (any hand-tuned state) has no bundle. +interface PresetBundle { visibleLevels: LogLevel[]; maxLines: number; autoScroll: boolean; wrap: boolean } +const LOG_PRESETS: { id: Exclude; label: string; hint: string; bundle: PresetBundle }[] = [ + { id: 'dev', label: '开发', hint: '全部级别 · 2000 行 · 换行', bundle: { visibleLevels: [...LEVELS], maxLines: 2000, autoScroll: true, wrap: true } }, + { id: 'ops', label: '运维', hint: 'INFO 以上 · 1000 行', bundle: { visibleLevels: ['info', 'success', 'warn', 'error'], maxLines: 1000, autoScroll: true, wrap: false } }, + { id: 'minimal', label: '精简', hint: '仅 WARN/ERROR · 500 行', bundle: { visibleLevels: ['warn', 'error'], maxLines: 500, autoScroll: true, wrap: false } }, +]; + // Highlight palette — keyword rules tint a matching row. Stored as an id. const HIGHLIGHT_COLORS: { id: string; label: string; swatch: string }[] = [ { id: 'amber', label: '琥珀', swatch: '#f59e0b' }, @@ -164,17 +173,38 @@ export function LogsPage() { }); }, [logs, filter, enabled, maxLines]); + // Any hand edit to the bundled prefs drops the active preset to 'custom'. const toggleLevel = (lv: LogLevel) => { const next = new Set(enabled); if (next.has(lv)) next.delete(lv); else next.add(lv); - setPages({ logs: { ...prefs, visibleLevels: LEVELS.filter((l) => next.has(l)) } }); + setPages({ logs: { ...prefs, visibleLevels: LEVELS.filter((l) => next.has(l)), preset: 'custom' } }); }; const clearFilters = useCallback(() => { setFilter(''); - setPages({ logs: { ...prefs, visibleLevels: [...LEVELS] } }); + setPages({ logs: { ...prefs, visibleLevels: [...LEVELS], preset: 'custom' } }); }, [prefs, setPages]); + const applyPreset = useCallback((p: typeof LOG_PRESETS[number]) => { + setPages({ logs: { ...prefs, ...p.bundle, preset: p.id } }); + }, [prefs, setPages]); + + // Dump the current (filtered) view to a .log text file — purely client-side. + const exportLogs = useCallback(() => { + if (filtered.length === 0) return; + const lines = filtered.map( + (l) => l.line || `${l.time} ${l.level.toUpperCase().padEnd(7)} [${l.scope}] ${l.message}`, + ); + const blob = new Blob([lines.join('\n') + '\n'], { type: 'text/plain;charset=utf-8' }); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + const ts = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19); + a.href = url; + a.download = `snowluma-logs-${ts}.log`; + a.click(); + URL.revokeObjectURL(url); + }, [filtered]); + const addHighlight = () => { const kw = newKeyword.trim(); if (!kw) return; @@ -244,14 +274,14 @@ export function LogsPage() { setPages({ logs: { ...prefs, autoScroll: !prefs.autoScroll } })} + onClick={() => setPages({ logs: { ...prefs, autoScroll: !prefs.autoScroll, preset: 'custom' } })} > {prefs.autoScroll ? : } setPages({ logs: { ...prefs, wrap: !prefs.wrap } })} + onClick={() => setPages({ logs: { ...prefs, wrap: !prefs.wrap, preset: 'custom' } })} > @@ -265,6 +295,9 @@ export function LogsPage() { void loadLogs()}> + + + setConfirmClear(true)}> @@ -310,6 +343,43 @@ export function LogsPage() { className="overflow-hidden" >
+ {/* View presets */} +
+
+ + 视图预设 + · 一键套用级别 / 行数 / 换行;手动调整后变为「自定义」 +
+
+ {LOG_PRESETS.map((p) => { + const active = prefs.preset === p.id; + return ( + + ); + })} + + 自定义 + +
+
+ {/* Server-side level */}
diff --git a/packages/webui/src/contexts/LayoutContext.tsx b/packages/webui/src/contexts/LayoutContext.tsx index 6ab9d979..58de1ae3 100644 --- a/packages/webui/src/contexts/LayoutContext.tsx +++ b/packages/webui/src/contexts/LayoutContext.tsx @@ -39,7 +39,7 @@ const ALL_LEVELS = ['trace', 'debug', 'info', 'success', 'warn', 'error']; export function defaultPages(): UiPages { return { defaultRoute: '/', - logs: { visibleLevels: [...ALL_LEVELS], maxLines: 1000, autoScroll: true, wrap: true, highlightRules: [] }, + logs: { visibleLevels: [...ALL_LEVELS], maxLines: 1000, autoScroll: true, wrap: true, highlightRules: [], preset: 'custom' }, processesSort: 'pid', configTab: '', }; diff --git a/packages/webui/src/types.ts b/packages/webui/src/types.ts index 565c4745..bad97e7c 100644 --- a/packages/webui/src/types.ts +++ b/packages/webui/src/types.ts @@ -249,12 +249,16 @@ export interface UiHighlightRule { color: string; } +export type LogsPreset = 'dev' | 'ops' | 'minimal' | 'custom'; + export interface UiLogsPrefs { visibleLevels: string[]; maxLines: number; autoScroll: boolean; wrap: boolean; highlightRules: UiHighlightRule[]; + /** Active view preset; 'custom' = hand-tuned. Client owns the bundles. */ + preset: LogsPreset; } export interface UiPages {