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 c9af41c
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 22 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 @@ -196,7 +196,7 @@ describe('CollectorMetricExporter - web', () => {

setTimeout(() => {
const response: any = spyLoggerError.args[0][0] as string;
assert.ok(response.includes('message: sendBeacon - cannot send'));
assert.ok(response.includes('sendBeacon - cannot send'));
assert.strictEqual(spyLoggerDebug.args.length, 1);

done();
Expand Down 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 @@ -155,7 +155,7 @@ describe('CollectorTraceExporter - web', () => {

setTimeout(() => {
const response = spyLoggerError.args[0][0] as string;
assert.ok(response.includes('message: sendBeacon - cannot send'));
assert.ok(response.includes('sendBeacon - cannot send'));
assert.strictEqual(spyLoggerDebug.args.length, 1);

done();
Expand Down 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
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ describe('CollectorMetricExporter - node with json over http', () => {
callback(mockResError);
setTimeout(() => {
const response = spyLoggerError.args[0][0] as string;

assert.ok(response.includes('code: 400'));
console.log(response);
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 @@ -177,7 +177,7 @@ describe('CollectorTraceExporter - node with json 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

0 comments on commit c9af41c

Please sign in to comment.