Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tolerate dirty json #431

Merged
merged 5 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"build": "tsc --pretty",
"lint": "eslint . --fix --ignore-path .gitignore",
"test": "node --test"
"test": "npm run build && node --test"
},
"repository": {
"url": "https://github.com/blacha/pretty-json-log"
Expand Down
8 changes: 5 additions & 3 deletions src/pretty/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ function getLogStatus(level: number): string {
return c.bgRed('FATAL');
}

type LogObject = Record<string, unknown>;

export const PrettySimplePino = {
Ignore: new Set<string>([
// pino formats
Expand All @@ -22,7 +24,7 @@ export const PrettySimplePino = {
'name',
'msg',
]),
formatObject(obj: Record<string, any>, prefix = ''): string[] {
formatObject(obj: LogObject, prefix = ''): string[] {
const kvs = [];
for (const key of Object.keys(obj)) {
if (PrettySimplePino.Ignore.has(key)) continue;
Expand All @@ -35,9 +37,9 @@ export const PrettySimplePino = {
if (typeofValue === 'number') {
output = c.yellow(String(value));
} else if (typeofValue === 'string') {
output = c.green(value);
output = c.green(value as string);
} else if (typeofValue === 'object') {
const subOutput = this.formatObject(value, prefix);
const subOutput = this.formatObject(value as LogObject, prefix);
if (subOutput.length > 0) {
output = `{ ${subOutput.join(' ')} }`;
}
Expand Down
23 changes: 23 additions & 0 deletions src/transform.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';

import { tryGetJson } from './transform.js';

describe('transform', () => {
describe('tryGetJson', () => {
it('returns null for invalid JSON', () => {
assert.strictEqual(tryGetJson(''), null);
assert.strictEqual(tryGetJson('foo'), null);
assert.strictEqual(tryGetJson('{"foo":'), null);
assert.strictEqual(tryGetJson('2024-01-01T19:53:58 {"foo":'), null);
assert.strictEqual(tryGetJson('2024-01-01T19:53:58 {"foo": "bar"}}'), null);
});

it('returns the parsed JSON', () => {
assert.deepStrictEqual(tryGetJson('{}'), {});
assert.deepStrictEqual(tryGetJson('{"foo": "bar"}'), { foo: 'bar' });
assert.deepStrictEqual(tryGetJson('2024-01-01T19:53:58 {"foo": "bar"}'), { foo: 'bar' });
assert.deepStrictEqual(tryGetJson('2024-01-01T19:53:58 {"foo": "bar"} garbage'), { foo: 'bar' });
});
});
});
10 changes: 8 additions & 2 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import { StringDecoder } from 'string_decoder';
import { LogMessage, LogSkipLine } from './msg.js';
import { PrettySimple } from './pretty/simple.js';

function tryGetJson(data: string): null | Record<string, unknown> {
export function tryGetJson(data: string): null | Record<string, unknown> {
const jsonStart = data.indexOf('{');
if (jsonStart === -1) return null;

const jsonEnd = data.lastIndexOf('}');
if (jsonEnd === -1) return null;

try {
return JSON.parse(data);
return JSON.parse(data.slice(jsonStart, jsonEnd + 1));
} catch {
return null;
}
Expand Down