From 21378ba3947df5cf800cce43c53e13d25b63cc71 Mon Sep 17 00:00:00 2001 From: Zachary Haber Date: Tue, 21 Jun 2022 10:44:35 -0500 Subject: [PATCH 1/3] chore: update readme and fix typo --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b578d58..2ff29cd 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,11 @@ logger.info("some interesting log message"); // global context variables can be modified anywhere in the app MDC.sessionID = 222; -logger.error("something has gone wrong", new Error("exception message")); +logger.error(new Error("exception message"), "something has gone wrong"); ``` -This example will result in two log events being sent to `lfs-server`. Both events will have a `requestId` property with a value of `123`. First event will have `sessionID` of `111` and second `sessionID` of `222`. Also since `enableCallStack` is set, both events will include location details such as file name, function name and line number. Second event will have a stack trace of a trown error. +This example will result in two log events being sent to `lfs-server`. Both events will have a `requestId` property with a value of `123`. The first event will have `sessionID` of `111` and second will have `sessionID` of `222`. Also since `enableCallStack` is set, both events will include location details such as file name, function name and line number. + +The second event will have a full stack trace of the `Error` object passed in. + +Without `enableCallStack`, the `Error` stack would still get logged, but not the file name, function name, and line number as those are parsed earlier. From 1aa644759551f5678e31ad16a58ebe6a1d4dc877 Mon Sep 17 00:00:00 2001 From: Zachary Haber Date: Tue, 21 Jun 2022 10:45:21 -0500 Subject: [PATCH 2/3] chore: enforce yml eol --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index 18ac215..4028aeb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -21,6 +21,7 @@ *.txt text eol=lf *.xml text eol=lf .husky/* text eol=lf +*.yml text eol=lf # Exclude the `.htaccess` file from GitHub's language statistics # https://github.com/github/linguist#using-gitattributes From 5860a79f085a047a2b2c30ab8f850dd2ef7c97ea Mon Sep 17 00:00:00 2001 From: Zachary Haber Date: Tue, 21 Jun 2022 14:48:38 -0500 Subject: [PATCH 3/3] fix: allow serializing of errors into message Only the first Error sent will avoid sending the full stack via `util.format` --- lib/index.js | 20 ++++++++++++++++---- test/tap/index-test.js | 19 +++++++++++-------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index 5853049..73b3e89 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,12 +8,24 @@ const util = require('util'); const axios = require('axios'); +/** + * + * @param {any[]} logData + * @returns {string} + */ function format(logData) { - // do not format error with stack, it is reported separetely - const filtered = logData.filter( - (item) => !(item instanceof Error && item.stack) + let foundError = false; + return util.format( + ...logData.map((item) => { + if (!foundError && item instanceof Error && item.stack) { + foundError = true; + // send only the base string for the first Error with a stack + // note: all errors on node have `stack` unless it's specifically deleted + return item.toString(); + } + return item; + }) ); - return util.format(...filtered); } function getErrorStack(logData) { diff --git a/test/tap/index-test.js b/test/tap/index-test.js index 7af2c81..86e619e 100644 --- a/test/tap/index-test.js +++ b/test/tap/index-test.js @@ -127,14 +127,17 @@ test('logFaces appender', (batch) => { const setup = setupLogging('stack-traces', false, { url: 'http://localhost/receivers/rx1', }); - - setup.logger.error('Oh no', new Error('something went wrong')); - const event = setup.fakeAxios.args[1]; - t.equal(event.m, 'Oh no'); - t.equal(event.w, true); - t.match(event.i, /Error: something went wrong/); - t.match(event.i, /at (Test.batch.test|Test.)/); - + const error = new Error('something went wrong'); + setup.logger.error(error, 'Oh no'); + let [, event] = setup.fakeAxios.args; + t.equal(event.m, `${error.toString()} Oh no`); + const eventErrorLog = { w: true, i: error.stack }; + t.match(event, eventErrorLog); + + setup.logger.error('Oh no', error); + [, event] = setup.fakeAxios.args; + t.equal(event.m, `Oh no ${error.toString()}`); + t.match(event, eventErrorLog); t.end(); });