Logs: fields, filters, live and export #244
No files matched your search
@@ -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<string, (viewName: string) => ReactNode> = {
|
||||
'x:Alert': (v) => <AlertsPage viewName={v} />,
|
||||
'x:NetworkListener': (v) => <ListenersPage viewName={v} />,
|
||||
'x:WebHook': (v) => <WebhooksPage viewName={v} />,
|
||||
// Fields split out, filters, live and export (#229)
|
||||
'x:Log': () => <LogsPage />,
|
||||
};
|
||||
|
||||
const unknown = (text: string) => (
|
||||
|
||||
@@ -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<LogFilterInput>(EMPTY_LOG_FILTER);
|
||||
const [filter, setFilter] = useState<LogFilterInput>(EMPTY_LOG_FILTER);
|
||||
const [entries, setEntries] = useState<LogEntry[]>([]);
|
||||
const [loaded, setLoaded] = useState<Keyed>({ 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<Set<string>>(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 (
|
||||
<span
|
||||
className="inline-block min-w-[4.5rem] rounded border px-1.5 py-0.5 text-center text-xs font-medium"
|
||||
style={color ? { color, borderColor: color } : undefined}
|
||||
>
|
||||
{levelLabel(level)}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<PageHeader
|
||||
icon="scroll-text"
|
||||
title={t('logs.title', 'Logs')}
|
||||
subtitle={pageAbout('x:Log')}
|
||||
actions={
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<Button
|
||||
variant={live ? 'default' : 'outline'}
|
||||
size="sm"
|
||||
onClick={() => setLive((v) => !v)}
|
||||
disabled={load.kind !== 'ready'}
|
||||
aria-pressed={live}
|
||||
>
|
||||
{live ? <Pause className="mr-1.5 h-4 w-4" /> : <Play className="mr-1.5 h-4 w-4" />}
|
||||
{live ? t('logs.pause', 'Pause') : t('logs.live', 'Follow live')}
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => setFetches((n) => n + 1)}
|
||||
disabled={load.kind === 'loading'}
|
||||
>
|
||||
<RotateCcw className="mr-1.5 h-4 w-4" />
|
||||
{t('logs.refresh', 'Refresh')}
|
||||
</Button>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="outline" size="sm" disabled={load.kind !== 'ready' || exporting}>
|
||||
{exporting ? (
|
||||
<Loader2 className="mr-1.5 h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<Download className="mr-1.5 h-4 w-4" />
|
||||
)}
|
||||
{t('logs.export', 'Save as file')}
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onSelect={() => void exportAs('jsonl')}>
|
||||
{t('logs.exportJson', 'JSON, one entry per line (.jsonl)')}
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onSelect={() => void exportAs('log')}>
|
||||
{t('logs.exportText', 'Plain text (.log)')}
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
|
||||
<form onSubmit={submit} className="grid gap-3 rounded-md border p-3 sm:grid-cols-2 lg:grid-cols-6">
|
||||
<label className="space-y-1 text-sm">
|
||||
<span className="text-muted-foreground">{t('logs.level', 'Level and above')}</span>
|
||||
<Select
|
||||
value={draft.level || ANY}
|
||||
onValueChange={(v) => setDraft((d) => ({ ...d, level: v === ANY ? '' : (v as LogLevel) }))}
|
||||
>
|
||||
<SelectTrigger aria-label={t('logs.level', 'Level and above')}>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value={ANY}>{t('logs.anyLevel', 'Any level')}</SelectItem>
|
||||
{LEVELS.map((level) => (
|
||||
<SelectItem key={level} value={level}>
|
||||
{levelLabel(level)}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</label>
|
||||
<label className="space-y-1 text-sm">
|
||||
<span className="text-muted-foreground">{t('logs.component', 'Component')}</span>
|
||||
<Input
|
||||
value={draft.component}
|
||||
placeholder={t('logs.componentHint', 'smtp, store, acme…')}
|
||||
onChange={(e) => setDraft((d) => ({ ...d, component: e.target.value }))}
|
||||
/>
|
||||
</label>
|
||||
<label className="space-y-1 text-sm lg:col-span-2">
|
||||
<span className="text-muted-foreground">{t('logs.text', 'Text')}</span>
|
||||
<Input value={draft.text} onChange={(e) => setDraft((d) => ({ ...d, text: e.target.value }))} />
|
||||
</label>
|
||||
<label className="space-y-1 text-sm">
|
||||
<span className="text-muted-foreground">{t('logs.from', 'From')}</span>
|
||||
<Input
|
||||
type="datetime-local"
|
||||
value={draft.from}
|
||||
onChange={(e) => setDraft((d) => ({ ...d, from: e.target.value }))}
|
||||
/>
|
||||
</label>
|
||||
<label className="space-y-1 text-sm">
|
||||
<span className="text-muted-foreground">{t('logs.to', 'To')}</span>
|
||||
<Input
|
||||
type="datetime-local"
|
||||
value={draft.to}
|
||||
onChange={(e) => setDraft((d) => ({ ...d, to: e.target.value }))}
|
||||
/>
|
||||
</label>
|
||||
<div className="flex gap-2 lg:col-span-6">
|
||||
<Button type="submit" size="sm">
|
||||
<Search className="mr-1.5 h-4 w-4" />
|
||||
{t('logs.show', 'Show')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={clear}
|
||||
disabled={!isFiltered(draft) && !isFiltered(filter)}
|
||||
>
|
||||
{t('logs.clear', 'Clear')}
|
||||
</Button>
|
||||
{live && (
|
||||
<span className="ml-auto self-center text-xs text-muted-foreground" role="status">
|
||||
{t('logs.following', 'Following live: new entries appear at the top.')}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{load.kind === 'noTracer' && <NoLogTracerNote />}
|
||||
{load.kind === 'error' && (
|
||||
<div role="alert" className="rounded-md border border-destructive/40 bg-destructive/5 p-3 text-sm">
|
||||
{load.message}
|
||||
</div>
|
||||
)}
|
||||
{load.kind === 'loading' && (
|
||||
<div className="flex items-center gap-2 p-6 text-sm text-muted-foreground">
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
{t('logs.loading', 'Reading the log…')}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{load.kind === 'ready' && entries.length === 0 && (
|
||||
<div className="rounded-lg border border-dashed p-10 text-center text-sm text-muted-foreground">
|
||||
{isFiltered(filter)
|
||||
? t('logs.noneFiltered', 'No entries match these filters.')
|
||||
: t('logs.none', 'No log entries yet.')}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{load.kind === 'ready' && entries.length > 0 && (
|
||||
<div className="overflow-x-auto rounded-md border">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-muted/50 text-left text-xs text-muted-foreground">
|
||||
<tr>
|
||||
<th className="w-8 px-2 py-2" />
|
||||
<th className="px-2 py-2 font-medium">{t('logs.colTime', 'Time')}</th>
|
||||
<th className="px-2 py-2 font-medium">{t('logs.colLevel', 'Level')}</th>
|
||||
<th className="px-2 py-2 font-medium">{t('logs.colEvent', 'Event')}</th>
|
||||
<th className="px-2 py-2 font-medium">{t('logs.colDetails', 'Details')}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{entries.map((entry) => {
|
||||
const isOpen = open.has(entry.id);
|
||||
const fields = parseFields(entry.details);
|
||||
return (
|
||||
<Fragment key={entry.id}>
|
||||
<tr
|
||||
className={cn('cursor-pointer border-t align-top hover:bg-muted/40', isOpen && 'bg-muted/30')}
|
||||
onClick={() => toggle(entry.id)}
|
||||
>
|
||||
<td className="px-2 py-1.5">
|
||||
<button
|
||||
type="button"
|
||||
className="text-muted-foreground"
|
||||
aria-expanded={isOpen}
|
||||
aria-label={isOpen ? t('logs.collapse', 'Hide fields') : t('logs.expand', 'Show fields')}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
toggle(entry.id);
|
||||
}}
|
||||
>
|
||||
{isOpen ? <ChevronDown className="h-4 w-4" /> : <ChevronRight className="h-4 w-4" />}
|
||||
</button>
|
||||
</td>
|
||||
<td className="whitespace-nowrap px-2 py-1.5 tabular-nums">{localTime(entry.timestamp)}</td>
|
||||
<td className="px-2 py-1.5">{levelBadge(entry.level)}</td>
|
||||
<td className="px-2 py-1.5">
|
||||
<div>{eventLabels.get(entry.event) ?? entry.event}</div>
|
||||
<div className="font-mono text-xs text-muted-foreground">{entry.event}</div>
|
||||
</td>
|
||||
<td className="max-w-[36rem] truncate px-2 py-1.5 font-mono text-xs text-muted-foreground">
|
||||
{entry.details}
|
||||
</td>
|
||||
</tr>
|
||||
{isOpen && (
|
||||
<tr className="border-t bg-muted/20">
|
||||
<td />
|
||||
<td colSpan={4} className="space-y-3 px-2 py-3">
|
||||
{fields.length > 0 ? (
|
||||
<dl className="grid grid-cols-[minmax(8rem,max-content)_1fr] gap-x-4 gap-y-1 text-xs">
|
||||
{fields.map((f, i) => (
|
||||
<Fragment key={`${f.key}-${i}`}>
|
||||
<dt className="font-mono text-muted-foreground">{f.key}</dt>
|
||||
<dd className="whitespace-pre-wrap break-all font-mono">{f.value}</dd>
|
||||
</Fragment>
|
||||
))}
|
||||
</dl>
|
||||
) : (
|
||||
<pre className="whitespace-pre-wrap break-all font-mono text-xs">
|
||||
{entry.details || t('logs.noFields', 'This entry has no fields.')}
|
||||
</pre>
|
||||
)}
|
||||
<div className="flex flex-wrap items-center gap-2 text-xs text-muted-foreground">
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
const component = componentOf(entry.event);
|
||||
setDraft((d) => ({ ...d, component }));
|
||||
setFilter((f) => ({ ...f, component }));
|
||||
}}
|
||||
>
|
||||
{t('logs.onlyComponent', 'Only {{component}}', { component: componentOf(entry.event) })}
|
||||
</Button>
|
||||
{canExplain && (
|
||||
<ExplainButton title={entry.event} subject={{ '@type': 'LogEntry', logId: entry.id }} />
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</Fragment>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{load.kind === 'ready' && more && (
|
||||
<div className="flex justify-center">
|
||||
<Button variant="outline" size="sm" onClick={() => void older()} disabled={loadingOlder}>
|
||||
{loadingOlder && <Loader2 className="mr-1.5 h-4 w-4 animate-spin" />}
|
||||
{t('logs.older', 'Show older entries')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -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 = ["[email protected]", "[email protected]"], causedBy = Store error (store.error) { reason = "x, y" }, text = "one, two"',
|
||||
),
|
||||
).toEqual([
|
||||
{ key: 'to', value: '["[email protected]", "[email protected]"]' },
|
||||
{ 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');
|
||||
});
|
||||
});
|
||||
@@ -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 `<time> <LEVEL> <description> (<event id>) <fields>`,
|
||||
* and the server hands the fields back as one string; `parseFields` splits
|
||||
* it into the key = value pairs the tracer wrote.
|
||||
*/
|
||||
|
||||
import { getAccountId, jmapQueryAndGet } from '@/services/jmap/client';
|
||||
import { listErrorOf } from '@/components/lists/listError';
|
||||
|
||||
export const LEVELS = ['error', 'warn', 'info', 'debug', 'trace'] as const;
|
||||
export type LogLevel = (typeof LEVELS)[number];
|
||||
|
||||
export interface LogEntry {
|
||||
id: string;
|
||||
/** RFC 3339, UTC. */
|
||||
timestamp: string;
|
||||
level: LogLevel;
|
||||
/** The event id, such as `smtp.connection-start`. */
|
||||
event: string;
|
||||
/** Everything after the event id, as the tracer wrote it. */
|
||||
details: string;
|
||||
}
|
||||
|
||||
export interface LogField {
|
||||
key: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
/** What the page filters by. Empty strings mean "any". */
|
||||
export interface LogFilterInput {
|
||||
/** This level and everything more severe. */
|
||||
level: LogLevel | '';
|
||||
/** A component (`smtp`) or a whole event id (`smtp.connection-start`). */
|
||||
component: string;
|
||||
text: string;
|
||||
/** `yyyy-mm-ddThh:mm`, local time, from a datetime-local input. */
|
||||
from: string;
|
||||
to: string;
|
||||
}
|
||||
|
||||
export const EMPTY_LOG_FILTER: LogFilterInput = { level: '', component: '', text: '', from: '', to: '' };
|
||||
|
||||
export const PAGE_SIZE = 100;
|
||||
/** The most entries one export holds. */
|
||||
export const EXPORT_MAX = 5000;
|
||||
const EXPORT_PAGE = 250;
|
||||
|
||||
/** A local `datetime-local` value as UTC RFC 3339, whole seconds. */
|
||||
function toUtc(local: string): string | null {
|
||||
const ms = Date.parse(local);
|
||||
if (Number.isNaN(ms)) return null;
|
||||
return new Date(ms).toISOString().replace(/\.\d{3}Z$/, 'Z');
|
||||
}
|
||||
|
||||
/** The server's filter: every condition must hold. */
|
||||
export function toServerFilter(input: LogFilterInput): Record<string, string> {
|
||||
const filter: Record<string, string> = {};
|
||||
if (input.level) filter.level = input.level;
|
||||
if (input.component.trim()) filter.event = input.component.trim();
|
||||
if (input.text.trim()) filter.text = input.text.trim();
|
||||
const from = input.from && toUtc(input.from);
|
||||
if (from) filter.timestampIsGreaterThanOrEqual = from;
|
||||
const to = input.to && toUtc(input.to);
|
||||
if (to) filter.timestampIsLessThanOrEqual = to;
|
||||
return filter;
|
||||
}
|
||||
|
||||
export function isFiltered(input: LogFilterInput): boolean {
|
||||
return Object.values(input).some((v) => v !== '');
|
||||
}
|
||||
|
||||
/** Why a page of logs couldn't be read. */
|
||||
export class LogsError extends Error {
|
||||
/** The server has no Log tracer, so no files to read. */
|
||||
readonly noTracer: boolean;
|
||||
constructor(message: string, noTracer = false) {
|
||||
super(message);
|
||||
this.noTracer = noTracer;
|
||||
}
|
||||
}
|
||||
|
||||
function normalize(raw: Record<string, unknown>): LogEntry {
|
||||
const level = String(raw.level ?? 'info').toLowerCase();
|
||||
return {
|
||||
id: String(raw.id),
|
||||
timestamp: String(raw.timestamp ?? ''),
|
||||
level: (LEVELS as readonly string[]).includes(level) ? (level as LogLevel) : 'info',
|
||||
event: String(raw.event ?? ''),
|
||||
details: String(raw.details ?? ''),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Entries newest first: the first page, or the page after `anchor`. A page
|
||||
* shorter than `limit` is the last one.
|
||||
*/
|
||||
export async function queryLogs(
|
||||
input: LogFilterInput,
|
||||
anchor: string | null,
|
||||
limit: number = PAGE_SIZE,
|
||||
): Promise<LogEntry[]> {
|
||||
const filter = toServerFilter(input);
|
||||
const responses = await jmapQueryAndGet(
|
||||
'x:Log',
|
||||
getAccountId('x:Log'),
|
||||
{
|
||||
...(Object.keys(filter).length > 0 && { filter }),
|
||||
limit,
|
||||
...(anchor ? { anchor, anchorOffset: 1 } : { position: 0 }),
|
||||
},
|
||||
['id', 'timestamp', 'level', 'event', 'details'],
|
||||
);
|
||||
for (const [name, args] of responses) {
|
||||
if (name === 'error' || name.endsWith('/error')) {
|
||||
const err = listErrorOf('x:Log', (args ?? {}) as Record<string, unknown>, 'Unknown error');
|
||||
throw err.kind === 'noLogTracer' ? new LogsError('No log tracer', true) : new LogsError(err.message);
|
||||
}
|
||||
}
|
||||
const ids = ((responses[0]?.[1] as { ids?: string[] }) ?? {}).ids ?? [];
|
||||
const list = ((responses[1]?.[1] as { list?: Record<string, unknown>[] }) ?? {}).list ?? [];
|
||||
const byId = new Map(list.map((raw) => [String(raw.id), normalize(raw)]));
|
||||
return ids.map((id) => byId.get(id)).filter((e): e is LogEntry => !!e);
|
||||
}
|
||||
|
||||
/**
|
||||
* What live mode adds to the top: the entries of a fresh first page that are
|
||||
* newer than the newest one shown. If that one isn't on the fresh page (a
|
||||
* burst of more than a page, or the log files rotated and the ids moved on),
|
||||
* the fresh page replaces what's shown.
|
||||
*/
|
||||
export function mergeNewer(shown: LogEntry[], fresh: LogEntry[], max: number): LogEntry[] {
|
||||
if (shown.length === 0) return fresh;
|
||||
const at = fresh.findIndex((e) => e.id === shown[0].id);
|
||||
if (at === -1) return fresh;
|
||||
if (at === 0) return shown;
|
||||
return [...fresh.slice(0, at), ...shown].slice(0, max);
|
||||
}
|
||||
|
||||
/** Every entry the filter finds, newest first, up to `max`. */
|
||||
export async function collectForExport(input: LogFilterInput, max: number = EXPORT_MAX): Promise<LogEntry[]> {
|
||||
const out: LogEntry[] = [];
|
||||
let anchor: string | null = null;
|
||||
while (out.length < max) {
|
||||
const page = await queryLogs(input, anchor, Math.min(EXPORT_PAGE, max - out.length));
|
||||
out.push(...page);
|
||||
if (page.length < EXPORT_PAGE) break;
|
||||
anchor = page[page.length - 1].id;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** One JSON object per line, with the fields split out. */
|
||||
export function toJsonLines(entries: LogEntry[]): string {
|
||||
return entries
|
||||
.map((e) =>
|
||||
JSON.stringify({
|
||||
timestamp: e.timestamp,
|
||||
level: e.level,
|
||||
event: e.event,
|
||||
fields: Object.fromEntries(parseFields(e.details).map((f) => [f.key, f.value])),
|
||||
details: e.details,
|
||||
}),
|
||||
)
|
||||
.map((line) => line + '\n')
|
||||
.join('');
|
||||
}
|
||||
|
||||
/** The lines as the tracer wrote them, give or take the event's description. */
|
||||
export function toText(entries: LogEntry[]): string {
|
||||
return entries.map((e) => `${e.timestamp} ${e.level.toUpperCase()} (${e.event}) ${e.details}\n`).join('');
|
||||
}
|
||||
|
||||
/** The component of an event id: `smtp` for `smtp.connection-start`. */
|
||||
export function componentOf(event: string): string {
|
||||
const dot = event.indexOf('.');
|
||||
return dot === -1 ? event : event.slice(0, dot);
|
||||
}
|
||||
|
||||
/** Whether the quote at `i` can end a string: what follows could end a value. */
|
||||
function closesString(details: string, i: number): boolean {
|
||||
const next = details[i + 1];
|
||||
return (
|
||||
next === undefined ||
|
||||
next === ']' ||
|
||||
next === '}' ||
|
||||
details.startsWith(', ', i + 1) ||
|
||||
details.startsWith(' }', i + 1)
|
||||
);
|
||||
}
|
||||
|
||||
function unquote(value: string): string {
|
||||
if (value.length < 2 || !value.startsWith('"') || !value.endsWith('"')) return value;
|
||||
return value
|
||||
.slice(1, -1)
|
||||
.replace(/\\([nrt\\])/g, (_, c: string) => (c === 'n' ? '\n' : c === 'r' ? '\r' : c === 't' ? '\t' : '\\'));
|
||||
}
|
||||
|
||||
/**
|
||||
* The `key = value, key = value` pairs of a log line's details. Strings are
|
||||
* quoted, nested events sit in `{ }` and lists in `[ ]`, so a comma only
|
||||
* separates two fields outside all three. The tracer doesn't escape a quote
|
||||
* inside a string, so a quote ends one only when what follows could end a
|
||||
* value: the end, `, `, ` }`, `]` or `}`. Details that aren't pairs at all come back as no fields.
|
||||
*/
|
||||
export function parseFields(details: string): LogField[] {
|
||||
const parts: string[] = [];
|
||||
let depth = 0;
|
||||
let inString = false;
|
||||
let start = 0;
|
||||
for (let i = 0; i < details.length; i++) {
|
||||
const c = details[i];
|
||||
if (inString) {
|
||||
if (c === '\\') i++;
|
||||
else if (c === '"' && closesString(details, i)) inString = false;
|
||||
continue;
|
||||
}
|
||||
if (c === '"') inString = true;
|
||||
else if (c === '{' || c === '[') depth++;
|
||||
else if ((c === '}' || c === ']') && depth > 0) depth--;
|
||||
else if (c === ',' && depth === 0 && details[i + 1] === ' ') {
|
||||
parts.push(details.slice(start, i));
|
||||
start = i + 2;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
parts.push(details.slice(start));
|
||||
|
||||
const fields: LogField[] = [];
|
||||
for (const part of parts) {
|
||||
const eq = part.indexOf(' = ');
|
||||
if (eq <= 0) return [];
|
||||
const key = part.slice(0, eq).trim();
|
||||
if (!/^[A-Za-z][\w.-]*$/.test(key)) return [];
|
||||
fields.push({ key, value: unquote(part.slice(eq + 3).trim()) });
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
+32
-1
@@ -3075,6 +3075,37 @@
|
||||
"onHost": "Installed on the host:",
|
||||
"inContainers": "In containers, from the install folder:",
|
||||
"noTracerAdd": "To read the log here, add a tracer of type Log with a folder the server can write to.",
|
||||
"openTracers": "Open Tracers"
|
||||
"openTracers": "Open Tracers",
|
||||
"exported": "Saved {{count}} entries",
|
||||
"exportFailed": "Couldn’t save the log",
|
||||
"title": "Logs",
|
||||
"pause": "Pause",
|
||||
"live": "Follow live",
|
||||
"refresh": "Refresh",
|
||||
"export": "Save as file",
|
||||
"exportJson": "JSON, one entry per line (.jsonl)",
|
||||
"exportText": "Plain text (.log)",
|
||||
"level": "Level and above",
|
||||
"anyLevel": "Any level",
|
||||
"component": "Component",
|
||||
"componentHint": "smtp, store, acme…",
|
||||
"text": "Text",
|
||||
"from": "From",
|
||||
"to": "To",
|
||||
"show": "Show",
|
||||
"clear": "Clear",
|
||||
"following": "Following live: new entries appear at the top.",
|
||||
"loading": "Reading the log…",
|
||||
"noneFiltered": "No entries match these filters.",
|
||||
"none": "No log entries yet.",
|
||||
"colTime": "Time",
|
||||
"colLevel": "Level",
|
||||
"colEvent": "Event",
|
||||
"colDetails": "Details",
|
||||
"collapse": "Hide fields",
|
||||
"expand": "Show fields",
|
||||
"noFields": "This entry has no fields.",
|
||||
"onlyComponent": "Only {{component}}",
|
||||
"older": "Show older entries"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user