From f85fda5cfbacebb844cf6bb3dc73fc76534dd10a Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 18 Nov 2026 14:57:15 -0700 Subject: [PATCH] Logs: read the fields of an entry from a JSON log file A Log tracer can write JSON lines (inbuxa-server #456). For those entries the server hands back the event's fields as a JSON object in `details` instead of `key = value` text, so the Logs page reads them as written: strings as they are, numbers, lists and nested events as JSON. Text entries are parsed as before, and a file holding both reads both. The JSON export keeps a JSON entry's fields typed rather than as strings. Test: parseFields and jsonFields on JSON objects, other JSON and plain text; the export keeps typed fields. --- src/features/logs/logs.test.ts | 31 +++++++++++++++++++++++++++++++ src/features/logs/logs.ts | 28 ++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/features/logs/logs.test.ts b/src/features/logs/logs.test.ts index ade6359..c1a17c3 100644 --- a/src/features/logs/logs.test.ts +++ b/src/features/logs/logs.test.ts @@ -9,6 +9,7 @@ import { EMPTY_LOG_FILTER, componentOf, mergeNewer, + jsonFields, parseFields, toJsonLines, toServerFilter, @@ -52,6 +53,20 @@ describe('parseFields', () => { ]); }); + it('reads the JSON object of a tracer writing JSON lines', () => { + expect( + parseFields( + '{"to":["a@example.org"],"subject":"say \\"hi\\", now","size":12,"tls":true,"causedBy":{"type":"store.error"}}', + ), + ).toEqual([ + { key: 'to', value: '["a@example.org"]' }, + { key: 'subject', value: 'say "hi", now' }, + { key: 'size', value: '12' }, + { key: 'tls', value: 'true' }, + { key: 'causedBy', value: '{"type":"store.error"}' }, + ]); + }); + it('returns no fields for details that are not pairs', () => { expect(parseFields('')).toEqual([]); expect(parseFields('something went wrong')).toEqual([]); @@ -111,8 +126,24 @@ describe('export', () => { }); }); + it('keeps the typed fields of a JSON entry', () => { + const [line] = toJsonLines([entry('1', '{"size":12,"to":["a@example.org"]}')]) + .trimEnd() + .split('\n'); + expect(JSON.parse(line).fields).toEqual({ size: 12, to: ['a@example.org'] }); + }); + it('names an event id’s component', () => { expect(componentOf('smtp.connection-start')).toBe('smtp'); expect(componentOf('store')).toBe('store'); }); }); + +describe('jsonFields', () => { + it('reads only a JSON object', () => { + expect(jsonFields('{"size":12}')).toEqual({ size: 12 }); + expect(jsonFields('size = 12')).toBeNull(); + expect(jsonFields('{not json')).toBeNull(); + expect(jsonFields('[1]')).toBeNull(); + }); +}); diff --git a/src/features/logs/logs.ts b/src/features/logs/logs.ts index f7e8668..4b9f2bc 100644 --- a/src/features/logs/logs.ts +++ b/src/features/logs/logs.ts @@ -9,7 +9,8 @@ * `x:Log`, which reads the files a Log tracer writes on the node that * answers. Each line is `