Skip to content

Commit

Permalink
Implement better handling for error objects
Browse files Browse the repository at this point in the history
  • Loading branch information
atd-schubert committed May 9, 2019
1 parent 47a11e9 commit a3be6f2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/ServiceLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ class ServiceLogger {

error(message, additionalFields) {
if (this.silence) return;
if (message instanceof Error) {
this._moveMsgObject("error", message.message, Object.assign({}, message, {stack: message.stack}, additionalFields));
return;
}
this._moveMsgObject("error", message, additionalFields);
}

Expand Down
101 changes: 89 additions & 12 deletions test/unit/Log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ describe("Log UNIT", function() {
});

new Logger({
"productionMode": true,
"logDir": "logs",
"silence": false,
"loggerName": "dev",
"dockerMode": true,
"varKey": "TLOG",
"logFieldOptions": {
"log_type": "application",
"application_type": "service",
"service": "test-service"
productionMode: true,
logDir: "logs",
silence: false,
loggerName: "dev",
dockerMode: true,
varKey: "TLOG",
logFieldOptions: {
log_type: "application",
application_type: "service",
service: "test-service"
},
"serviceName": "test-service",
"level": "DEBUG"
serviceName: "test-service",
level: "DEBUG"
});

const cor_value = "i-am-a-correlation-id";
Expand All @@ -40,4 +40,81 @@ describe("Log UNIT", function() {
assert.ok(captured.includes(cor_value));
assert.equal(captured.split("\n").length, 4);
});

describe("Logging of error objects", () => {
const cor_value = "i-am-a-correlation-id";
const errorMessage = "Error message";
const additionalText = "this is an additional property";
const additionalProperty = "additionalPropertyOnError";

it("should log error objects", () => {
let captured = "";
new Logger({
productionMode: true,
logDir: "logs",
silence: false,
loggerName: "dev",
dockerMode: true,
varKey: "ELOG",
logFieldOptions: {
log_type: "application",
application_type: "service",
service: "test-service"
},
serviceName: "test-service",
level: "DEBUG"
});

const testLogger = ELOG.createChild({"correlation-id": cor_value});
const error = new Error(errorMessage);
error[additionalProperty] = additionalText;

const unhook_intercept = intercept(txt => captured += txt);
testLogger.error(error);

unhook_intercept();

assert.ok(captured.includes(`"${additionalProperty}":"${additionalText}"`), "includes the additional property");
assert.ok(captured.includes(`"stack":"Error: ${errorMessage}\\n`), "Includes the stacktrace");
assert.ok(captured.includes(`"msg":"${errorMessage}"`), "Includes the error message as log msg property");
assert.ok(captured.includes(`"correlation-id":"${cor_value}"`), "Includes the error message as log msg property");
});
it("should log extended error objects", () => {
let captured = "";
new Logger({
productionMode: true,
logDir: "logs",
silence: false,
loggerName: "dev",
dockerMode: true,
varKey: "ELOG",
logFieldOptions: {
log_type: "application",
application_type: "service",
service: "test-service"
},
serviceName: "test-service",
level: "DEBUG"
});

const testLogger = ELOG.createChild({"correlation-id": cor_value});
class ExtError extends Error {
constructor(message) {
super(message);
this[additionalProperty] = additionalText;
}
}
const error = new ExtError(errorMessage);

const unhook_intercept = intercept(txt => captured += txt);
testLogger.error(error);

unhook_intercept();

assert.ok(captured.includes(`"${additionalProperty}":"${additionalText}"`), "includes the additional property");
assert.ok(captured.includes(`"stack":"Error: ${errorMessage}\\n`), "Includes the stacktrace");
assert.ok(captured.includes(`"msg":"${errorMessage}"`), "Includes the error message as log msg property");
assert.ok(captured.includes(`"correlation-id":"${cor_value}"`), "Includes the error message as log msg property");
});
});
});

0 comments on commit a3be6f2

Please sign in to comment.