Logs: read the fields of an entry from a JSON log file #246
No files matched your search
@@ -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":["[email protected]"],"subject":"say \\"hi\\", now","size":12,"tls":true,"causedBy":{"type":"store.error"}}',
|
||||
),
|
||||
).toEqual([
|
||||
{ key: 'to', value: '["[email protected]"]' },
|
||||
{ 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":["[email protected]"]}')])
|
||||
.trimEnd()
|
||||
.split('\n');
|
||||
expect(JSON.parse(line).fields).toEqual({ size: 12, to: ['[email protected]'] });
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -9,7 +9,8 @@
|
||||
* `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.
|
||||
* it into the key = value pairs the tracer wrote. A tracer writing JSON lines
|
||||
* (inbuxa-server #456) hands them back as a JSON object instead, read as is.
|
||||
*/
|
||||
|
||||
import { getAccountId, jmapQueryAndGet } from '@/services/jmap/client';
|
||||
@@ -165,7 +166,7 @@ export function toJsonLines(entries: LogEntry[]): string {
|
||||
timestamp: e.timestamp,
|
||||
level: e.level,
|
||||
event: e.event,
|
||||
fields: Object.fromEntries(parseFields(e.details).map((f) => [f.key, f.value])),
|
||||
fields: jsonFields(e.details) ?? Object.fromEntries(parseFields(e.details).map((f) => [f.key, f.value])),
|
||||
details: e.details,
|
||||
}),
|
||||
)
|
||||
@@ -203,6 +204,22 @@ function unquote(value: string): string {
|
||||
.replace(/\\([nrt\\])/g, (_, c: string) => (c === 'n' ? '\n' : c === 'r' ? '\r' : c === 't' ? '\t' : '\\'));
|
||||
}
|
||||
|
||||
/**
|
||||
* The fields of an entry from a tracer writing JSON lines: the server hands
|
||||
* them back as a JSON object, typed as written. Null for any other details.
|
||||
*/
|
||||
export function jsonFields(details: string): Record<string, unknown> | null {
|
||||
if (!details.startsWith('{')) return null;
|
||||
try {
|
||||
const value: unknown = JSON.parse(details);
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -211,6 +228,13 @@ function unquote(value: string): string {
|
||||
* value: the end, `, `, ` }`, `]` or `}`. Details that aren't pairs at all come back as no fields.
|
||||
*/
|
||||
export function parseFields(details: string): LogField[] {
|
||||
const json = jsonFields(details);
|
||||
if (json) {
|
||||
return Object.entries(json).map(([key, value]) => ({
|
||||
key,
|
||||
value: typeof value === 'string' ? value : JSON.stringify(value),
|
||||
}));
|
||||
}
|
||||
const parts: string[] = [];
|
||||
let depth = 0;
|
||||
let inString = false;
|
||||
|
||||
Reference in new issue
Block a user