From dc5b7450513287651f1b2d957043abc47c2f4659 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 18 Nov 2026 12:53:19 -0700 Subject: [PATCH] Logs: fields, filters, live and export The Logs page was the generic list: one long line per entry and a text filter. It's now a page of its own: - each entry opens to show its fields (key = value, split the way the Log tracer writes them, nested events and lists kept whole), with "Only " and Explain this; - levels carry the schema's colors; - filters for level and above, component, text and a from/to time; - Follow live asks for the newest page every 5 seconds while the tab is visible, with Pause; it keeps up to 1,000 entries and starts over when the log files rotate; - Save as file writes up to 5,000 filtered entries as JSON lines (fields split out) or plain text. A server with no Log tracer still gets the note from #217. The level, component and time filters need inbuxa-server #455; text and everything else work with older servers. Test: unit tests for the field parser (quotes, lists, nested events, an unescaped quote), the filter, live merging and export. --- src/components/layout/MainContent.tsx | 3 + src/features/logs/LogsPage.tsx | 459 ++++++++++++++++++++++++++ src/features/logs/logs.test.ts | 118 +++++++ src/features/logs/logs.ts | 245 ++++++++++++++ src/i18n/en.json | 33 +- 5 files changed, 857 insertions(+), 1 deletion(-) create mode 100644 src/features/logs/LogsPage.tsx create mode 100644 src/features/logs/logs.test.ts create mode 100644 src/features/logs/logs.ts diff --git a/src/components/layout/MainContent.tsx b/src/components/layout/MainContent.tsx index 95320e9..9be412b 100644 --- a/src/components/layout/MainContent.tsx +++ b/src/components/layout/MainContent.tsx @@ -23,6 +23,7 @@ import { TlsSummaryCard } from '@/features/incomingReports/TlsSummaryCard'; import { LimitsSummary } from '@/features/limits/LimitsSummary'; import type { LimitObject } from '@/features/limits/rules'; import { MetricsPage } from '@/features/metricsConnect/MetricsPage'; +import { LogsPage } from '@/features/logs/LogsPage'; import { ListenersPage } from '@/features/ports/ListenersPage'; import { ReportsPage } from '@/features/reports/ReportsPage'; import { RetentionPage } from '@/features/retention/RetentionPage'; @@ -247,6 +248,8 @@ const LIST_PAGES: Record ReactNode> = { 'x:Alert': (v) => , 'x:NetworkListener': (v) => , 'x:WebHook': (v) => , + // Fields split out, filters, live and export (#229) + 'x:Log': () => , }; const unknown = (text: string) => ( diff --git a/src/features/logs/LogsPage.tsx b/src/features/logs/LogsPage.tsx new file mode 100644 index 0000000..1419a9d --- /dev/null +++ b/src/features/logs/LogsPage.tsx @@ -0,0 +1,459 @@ +/* + * SPDX-FileCopyrightText: 2026 Coffey Labs LLC + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +/** + * INBUXA: Management › Logs (inbuxa-admin #229, forum t/308). The server's + * log entries with their fields split out, filtered by level, component, + * text and time, followed live, and saved as a file. + * + * Entries come from the Log tracer's files on the node that answered. Live + * mode asks for the newest page every few seconds while the tab is visible; + * it needs nothing from the server beyond the query the page already makes. + */ + +import { Fragment, useCallback, useEffect, useMemo, useState, type FormEvent } from 'react'; +import { useTranslation } from 'react-i18next'; +import { ChevronDown, ChevronRight, Download, Loader2, Pause, Play, RotateCcw, Search } from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuTrigger, +} from '@/components/ui/dropdown-menu'; +import { PageHeader } from '@/components/common/PageHeader'; +import { NoLogTracerNote } from '@/components/lists/NoLogTracerNote'; +import { ExplainButton } from '@/features/ai/explain/ExplainButton'; +import { useExplainStore } from '@/features/ai/explain/explainStore'; +import { pageAbout } from '@/help/texts'; +import { toast } from '@/hooks/use-toast'; +import { cn } from '@/lib/utils'; +import { useSchemaStore } from '@/stores/schemaStore'; +import { + EMPTY_LOG_FILTER, + LEVELS, + LogsError, + PAGE_SIZE, + collectForExport, + componentOf, + isFiltered, + mergeNewer, + parseFields, + queryLogs, + toJsonLines, + toText, + type LogEntry, + type LogFilterInput, + type LogLevel, +} from './logs'; + +/** How often live mode asks for new entries. */ +const LIVE_INTERVAL_MS = 5000; +/** The most entries the page keeps while following live. */ +const MAX_SHOWN = 1000; +const ANY = '__any__'; + +type Load = { kind: 'loading' } | { kind: 'ready' } | { kind: 'noTracer' } | { kind: 'error'; message: string }; +/** A load, and the filter and Refresh it answers. */ +type Keyed = { key: string; load: Load }; + +function download(name: string, body: string, type: string) { + const href = URL.createObjectURL(new Blob([body], { type })); + const a = document.createElement('a'); + a.href = href; + a.download = name; + document.body.appendChild(a); + a.click(); + a.remove(); + setTimeout(() => URL.revokeObjectURL(href), 10_000); +} + +function localTime(ts: string): string { + const ms = Date.parse(ts); + return Number.isNaN(ms) ? ts : new Date(ms).toLocaleString(); +} + +export function LogsPage() { + const { t } = useTranslation(); + const schema = useSchemaStore((s) => s.schema); + // "Explain this" on each entry (ai-explain EX-17) + const canExplain = useExplainStore((s) => s.available); + + const levelInfo = useMemo(() => { + const variants = schema?.enums?.TracingLevel ?? []; + return new Map(variants.map((v) => [v.name, v])); + }, [schema]); + const eventLabels = useMemo(() => { + const variants = schema?.enums?.EventType ?? []; + return new Map(variants.map((v) => [v.name, v.label])); + }, [schema]); + + const [draft, setDraft] = useState(EMPTY_LOG_FILTER); + const [filter, setFilter] = useState(EMPTY_LOG_FILTER); + const [entries, setEntries] = useState([]); + const [loaded, setLoaded] = useState({ key: '', load: { kind: 'loading' } }); + const [more, setMore] = useState(false); + const [loadingOlder, setLoadingOlder] = useState(false); + const [live, setLive] = useState(false); + const [exporting, setExporting] = useState(false); + const [open, setOpen] = useState>(new Set()); + const [fetches, setFetches] = useState(0); + const key = `${fetches}:${JSON.stringify(filter)}`; + // Until the answer for this filter arrives, the page is loading + const load: Load = loaded.key === key ? loaded.load : { kind: 'loading' }; + + const failure = useCallback( + (err: unknown): Load => + err instanceof LogsError && err.noTracer + ? { kind: 'noTracer' } + : { kind: 'error', message: err instanceof Error ? err.message : t('list.unknownError', 'Unknown error') }, + [t], + ); + + // The first page, again whenever the filter changes or Refresh is pressed + useEffect(() => { + let cancelled = false; + queryLogs(filter, null) + .then((page) => { + if (cancelled) return; + setEntries(page); + setMore(page.length === PAGE_SIZE); + setOpen(new Set()); + setLoaded({ key, load: { kind: 'ready' } }); + }) + .catch((err) => !cancelled && setLoaded({ key, load: failure(err) })); + return () => { + cancelled = true; + }; + }, [filter, key, failure]); + + // Live: new entries on top while the tab is visible + useEffect(() => { + if (!live || load.kind !== 'ready') return; + let busy = false; + const timer = window.setInterval(() => { + if (busy || document.hidden) return; + busy = true; + queryLogs(filter, null) + .then((fresh) => setEntries((shown) => mergeNewer(shown, fresh, MAX_SHOWN))) + .catch(() => setLive(false)) + .finally(() => { + busy = false; + }); + }, LIVE_INTERVAL_MS); + return () => window.clearInterval(timer); + }, [live, load.kind, filter]); + + const older = async () => { + const last = entries[entries.length - 1]; + if (!last) return; + setLoadingOlder(true); + try { + const page = await queryLogs(filter, last.id); + setEntries((shown) => [...shown, ...page.filter((e) => !shown.some((s) => s.id === e.id))]); + setMore(page.length === PAGE_SIZE); + } catch (err) { + setLoaded({ key, load: failure(err) }); + } finally { + setLoadingOlder(false); + } + }; + + const exportAs = async (format: 'jsonl' | 'log') => { + setExporting(true); + try { + const all = await collectForExport(filter); + const stamp = new Date().toISOString().slice(0, 19).replace(/[:T]/g, '-'); + if (format === 'jsonl') download(`inbuxa-log-${stamp}.jsonl`, toJsonLines(all), 'application/x-ndjson'); + else download(`inbuxa-log-${stamp}.log`, toText(all), 'text/plain'); + toast({ title: t('logs.exported', 'Saved {{count}} entries', { count: all.length }) }); + } catch (err) { + toast({ + title: t('logs.exportFailed', 'Couldn’t save the log'), + description: err instanceof Error ? err.message : undefined, + variant: 'destructive', + }); + } finally { + setExporting(false); + } + }; + + const submit = (e: FormEvent) => { + e.preventDefault(); + setFilter({ ...draft }); + }; + const clear = () => { + setDraft(EMPTY_LOG_FILTER); + setFilter(EMPTY_LOG_FILTER); + }; + const toggle = (id: string) => + setOpen((was) => { + const next = new Set(was); + if (next.has(id)) next.delete(id); + else next.add(id); + return next; + }); + + const levelLabel = (level: LogLevel) => levelInfo.get(level)?.label ?? level; + const levelBadge = (level: LogLevel) => { + const color = levelInfo.get(level)?.color; + return ( + + {levelLabel(level)} + + ); + }; + + return ( +
+ + + + + + + + + void exportAs('jsonl')}> + {t('logs.exportJson', 'JSON, one entry per line (.jsonl)')} + + void exportAs('log')}> + {t('logs.exportText', 'Plain text (.log)')} + + + +
+ } + /> + +
+ + + + + +
+ + + {live && ( + + {t('logs.following', 'Following live: new entries appear at the top.')} + + )} +
+
+ + {load.kind === 'noTracer' && } + {load.kind === 'error' && ( +
+ {load.message} +
+ )} + {load.kind === 'loading' && ( +
+ + {t('logs.loading', 'Reading the log…')} +
+ )} + + {load.kind === 'ready' && entries.length === 0 && ( +
+ {isFiltered(filter) + ? t('logs.noneFiltered', 'No entries match these filters.') + : t('logs.none', 'No log entries yet.')} +
+ )} + + {load.kind === 'ready' && entries.length > 0 && ( +
+ + + + + + + + + + + {entries.map((entry) => { + const isOpen = open.has(entry.id); + const fields = parseFields(entry.details); + return ( + + toggle(entry.id)} + > + + + + + + + {isOpen && ( + + + + )} + + ); + })} + +
+ {t('logs.colTime', 'Time')}{t('logs.colLevel', 'Level')}{t('logs.colEvent', 'Event')}{t('logs.colDetails', 'Details')}
+ + {localTime(entry.timestamp)}{levelBadge(entry.level)} +
{eventLabels.get(entry.event) ?? entry.event}
+
{entry.event}
+
+ {entry.details} +
+ + {fields.length > 0 ? ( +
+ {fields.map((f, i) => ( + +
{f.key}
+
{f.value}
+
+ ))} +
+ ) : ( +
+                              {entry.details || t('logs.noFields', 'This entry has no fields.')}
+                            
+ )} +
+ + {canExplain && ( + + )} +
+
+
+ )} + + {load.kind === 'ready' && more && ( +
+ +
+ )} + + ); +} diff --git a/src/features/logs/logs.test.ts b/src/features/logs/logs.test.ts new file mode 100644 index 0000000..ade6359 --- /dev/null +++ b/src/features/logs/logs.test.ts @@ -0,0 +1,118 @@ +/* + * SPDX-FileCopyrightText: 2026 Coffey Labs LLC + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { describe, expect, it } from 'vitest'; +import { + EMPTY_LOG_FILTER, + componentOf, + mergeNewer, + parseFields, + toJsonLines, + toServerFilter, + type LogEntry, +} from './logs'; + +const entry = (id: string, details = ''): LogEntry => ({ + id, + timestamp: '2026-11-18T10:00:00Z', + level: 'info', + event: 'smtp.connection-start', + details, +}); + +describe('parseFields', () => { + it('splits key = value pairs and unquotes strings', () => { + expect(parseFields('listenerId = "smtp", remoteIp = 192.0.2.1, size = 1024, details = "a\\nb"')).toEqual([ + { key: 'listenerId', value: 'smtp' }, + { key: 'remoteIp', value: '192.0.2.1' }, + { key: 'size', value: '1024' }, + { key: 'details', value: 'a\nb' }, + ]); + }); + + it('keeps commas inside strings, lists and nested events', () => { + expect( + parseFields( + 'to = ["a@example.org", "b@example.org"], causedBy = Store error (store.error) { reason = "x, y" }, text = "one, two"', + ), + ).toEqual([ + { key: 'to', value: '["a@example.org", "b@example.org"]' }, + { key: 'causedBy', value: 'Store error (store.error) { reason = "x, y" }' }, + { key: 'text', value: 'one, two' }, + ]); + }); + + it('copes with an unescaped quote inside a string', () => { + expect(parseFields('subject = "say "hi" now", size = 3')).toEqual([ + { key: 'subject', value: 'say "hi" now' }, + { key: 'size', value: '3' }, + ]); + }); + + it('returns no fields for details that are not pairs', () => { + expect(parseFields('')).toEqual([]); + expect(parseFields('something went wrong')).toEqual([]); + }); +}); + +describe('toServerFilter', () => { + it('sends only what was picked', () => { + expect(toServerFilter(EMPTY_LOG_FILTER)).toEqual({}); + expect(toServerFilter({ ...EMPTY_LOG_FILTER, level: 'warn', component: ' smtp ', text: 'bounce' })).toEqual({ + level: 'warn', + event: 'smtp', + text: 'bounce', + }); + }); + + it('turns a local time range into UTC bounds', () => { + const filter = toServerFilter({ ...EMPTY_LOG_FILTER, from: '2026-11-18T10:00', to: '2026-11-18T11:30' }); + expect(filter.timestampIsGreaterThanOrEqual).toBe(new Date('2026-11-18T10:00').toISOString().replace('.000', '')); + expect(filter.timestampIsLessThanOrEqual).toBe(new Date('2026-11-18T11:30').toISOString().replace('.000', '')); + }); +}); + +describe('mergeNewer', () => { + it('puts what is new on top', () => { + const shown = [entry('3'), entry('2'), entry('1')]; + expect(mergeNewer(shown, [entry('5'), entry('4'), entry('3'), entry('2')], 10).map((e) => e.id)).toEqual([ + '5', + '4', + '3', + '2', + '1', + ]); + }); + + it('keeps what is shown when nothing is new, and caps the length', () => { + const shown = [entry('2'), entry('1')]; + expect(mergeNewer(shown, [entry('2'), entry('1')], 10)).toBe(shown); + expect(mergeNewer(shown, [entry('4'), entry('3'), entry('2')], 3).map((e) => e.id)).toEqual(['4', '3', '2']); + }); + + it('starts over when the newest shown entry is gone from the fresh page', () => { + const fresh = [entry('9'), entry('8')]; + expect(mergeNewer([entry('2')], fresh, 10)).toBe(fresh); + }); +}); + +describe('export', () => { + it('writes one JSON object per line with the fields split out', () => { + const [line] = toJsonLines([entry('1', 'remoteIp = 192.0.2.1')]) + .trimEnd() + .split('\n'); + expect(JSON.parse(line)).toMatchObject({ + level: 'info', + event: 'smtp.connection-start', + fields: { remoteIp: '192.0.2.1' }, + }); + }); + + it('names an event id’s component', () => { + expect(componentOf('smtp.connection-start')).toBe('smtp'); + expect(componentOf('store')).toBe('store'); + }); +}); diff --git a/src/features/logs/logs.ts b/src/features/logs/logs.ts new file mode 100644 index 0000000..f7e8668 --- /dev/null +++ b/src/features/logs/logs.ts @@ -0,0 +1,245 @@ +/* + * SPDX-FileCopyrightText: 2026 Coffey Labs LLC + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +/** + * INBUXA: the Logs page's data (inbuxa-admin #229). Entries come from + * `x:Log`, which reads the files a Log tracer writes on the node that + * answers. Each line is `