Skip to content

Commit

Permalink
refactor: use JSON.stringify to format log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Sep 16, 2020
1 parent 2673088 commit 10d8743
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ function stringifyException(ex: Exception | string): string {
if (typeof ex === 'string') {
return ex;
} else {
return Object.entries(flattenException(ex))
.map(([k, v]) => `${k}: ${v}`)
.join('\n');
return JSON.stringify(flattenException(ex));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ describe('loggingErrorHandler', () => {

const [result] = errorStub.lastCall.args;

assert.ok(result.includes(`name: ${err.name}`));
assert.ok(result.includes(`message: ${err.message}`));
assert.ok(result.includes(`randomString: ${err.randomString}`));
assert.ok(result.includes(`randomNumber: ${err.randomNumber}`));
assert.ok(result.includes(`randomArray: ${err.randomArray}`));
assert.ok(result.includes(`randomObject: ${err.randomObject}`));
assert.ok(result.includes(`stack: ${err.stack}`));
assert.ok(result.includes(err.name));
assert.ok(result.includes(err.message));
assert.ok(result.includes(err.randomString));
assert.ok(result.includes(err.randomNumber));
assert.ok(result.includes(err.randomArray));
assert.ok(result.includes(err.randomObject));
assert.ok(result.includes(JSON.stringify(err.stack)));
});

it('logs from an error', () => {
Expand All @@ -68,8 +68,8 @@ describe('loggingErrorHandler', () => {

const [result] = errorStub.lastCall.args;

assert.ok(result.includes(`name: ${err.name}`));
assert.ok(result.includes(`message: ${err.message}`));
assert.ok(result.includes(`stack: ${err.stack}`));
assert.ok(result.includes(err.name));
assert.ok(result.includes(err.message));
assert.ok(result.includes(JSON.stringify(err.stack)));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('CollectorMetricExporter - node with proto over http', () => {
callback(mockResError);
setTimeout(() => {
const response = spyLoggerError.args[0][0] as string;
assert.ok(response.includes('code: 400'));
assert.ok(response.includes('"code":"400"'));
assert.strictEqual(responseSpy.args[0][0], 1);
done();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe('CollectorTraceExporter - node with proto over http', () => {
setTimeout(() => {
const response = spyLoggerError.args[0][0] as string;

assert.ok(response.includes('code: 400'));
assert.ok(response.includes('"code":"400"'));
assert.strictEqual(responseSpy.args[0][0], 1);
done();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ describe('CollectorMetricExporter - web', () => {
request.respond(400);

const response = spyLoggerError.args[0][0] as string;
assert.ok(response.includes('code: 400'));
assert.ok(response.includes('"code":"400"'));

assert.strictEqual(spyBeacon.callCount, 0);
done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe('CollectorTraceExporter - web', () => {

const response = spyLoggerError.args[0][0] as string;

assert.ok(response.includes('code: 400'));
assert.ok(response.includes('"code":"400"'));

assert.strictEqual(spyBeacon.callCount, 0);
done();
Expand Down

0 comments on commit 10d8743

Please sign in to comment.