Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,17 @@ function processData(data: Uint8Array | string, encoding?: BufferEncoding) {
// strip any leading ANSI color codes from the decoded data
// to parse colored JSON objects correctly
decodedData = decodedData.replace(/\x1b[[(?);]{0,2}(;?\d)*./g, '');
let parsedData;
try {
return {isJSON: true, processedData: JSON.parse(decodedData)};
parsedData = JSON.parse(decodedData);
} catch (e) {
return {isJSON: false, processedData: decodedData};
}
// Numbers, booleans, null and JSON strings parse successfully but they are
// not log objects, so there is nowhere to attach the context fields added by
// getJSONWithContext. Log them as plain text instead.
if (typeof parsedData !== 'object' || parsedData === null) {
return {isJSON: false, processedData: decodedData};
}
return {isJSON: true, processedData: parsedData};
}
35 changes: 35 additions & 0 deletions test/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,41 @@ describe('getModifiedData', () => {
assert.equal(modifiedData, expectedJSONOutput);
});

it('json number value', () => {
const modifiedData = getModifiedData('42');
assert.equal(
modifiedData,
JSON.stringify(Object.assign({message: '42'}, expectedMetadata)) + '\n',
);
});

it('json boolean value', () => {
const modifiedData = getModifiedData('true');
assert.equal(
modifiedData,
JSON.stringify(Object.assign({message: 'true'}, expectedMetadata)) +
'\n',
);
});

it('json null value', () => {
const modifiedData = getModifiedData('null');
assert.equal(
modifiedData,
JSON.stringify(Object.assign({message: 'null'}, expectedMetadata)) +
'\n',
);
});

it('json string value', () => {
const modifiedData = getModifiedData('"abc"');
assert.equal(
modifiedData,
JSON.stringify(Object.assign({message: '"abc"'}, expectedMetadata)) +
'\n',
);
});

it('json with user label', () => {
const data = JSON.stringify({
text: 'default text.',
Expand Down
Loading