Skip to content

Commit 85f8916

Browse files
Feature/fix logger (#14)
* Fix context info * bump dependencies * add traceId as tag
1 parent 23294f8 commit 85f8916

File tree

4 files changed

+86
-99
lines changed

4 files changed

+86
-99
lines changed

package-lock.json

Lines changed: 64 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "quintoandar-logger",
3-
"version": "3.2.0",
3+
"version": "3.3.0",
44
"description": "Winston logger with custom 5A configuration",
55
"main": "src/main.js",
66
"dependencies": {
7-
"@sentry/node": "^5.19.2",
8-
"lodash": "^4.17.19",
7+
"@sentry/node": "^5.27.2",
8+
"lodash": "^4.17.20",
99
"stack-trace": "0.0.10",
1010
"util": "^0.12.3",
1111
"winston": "^3.3.3",

src/main.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,22 @@ function formatParams(params, module, funcCallerParam) {
7474
}
7575

7676
for (let i = 1; i < params.length; i++) {
77-
if (params[i] instanceof Error) {
77+
if (params[i] instanceof Error && !metadata.error) {
7878
metadata.error = params[i];
7979
} else if (params[i] && typeof params[i] === 'object') {
8080
if (!metadata.extra) {
8181
metadata.extra = {};
8282
}
83-
const name = (params[i].constructor && params[i].constructor.name) || params[i].name
83+
const name = (params[i].constructor && params[i].constructor.name) || params[i].name;
8484

8585
if (name) {
86-
if (name in metadata.extra) {
87-
Object.assign(metadata.extra[name], params[i])
88-
}
89-
else {
90-
metadata.extra[name] = params[i]
91-
}
86+
let suffix = 0
87+
for (; name + '.' + suffix.toString() in metadata.extra; suffix++) { }
88+
89+
metadata.extra[name + '.' + suffix.toString()] = params[i];
9290
}
9391
else {
94-
Object.assign(metadata.extra, params[i]);
92+
metadata.extra = Object.assign(metadata.extra, params[i]);
9593
}
9694
}
9795
}

src/transports.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,21 @@ class SentryTransport extends Transport {
4949
info.error = util.inspect(error, { showHidden: false, depth: null });
5050
}
5151

52-
Sentry.configureScope((scope) => {
52+
Sentry.withScope((scope) => {
5353
scope.setLevel(this._levelsMap[info.level]);
54-
Object.entries(info)
55-
.filter(([ key ]) => ['error', 'level'].includes(key) === false)
56-
.forEach(([ key, value ]) => {
57-
scope.setExtra(key, value);
58-
})
54+
if (info.traceId) {
55+
scope.setTag("traceId", info.traceId)
56+
}
57+
scope.setContext(info)
58+
scope.setExtras(info);
59+
if (thereIsErrorExtraData) {
60+
Sentry.captureException(error);
61+
}
62+
else Sentry.captureMessage(info.message);
63+
5964
});
6065

61-
if (thereIsErrorExtraData) {
62-
Sentry.setExtra(error);
63-
Sentry.captureMessage(error);
64-
}
65-
else Sentry.captureMessage(info.message);
66+
6667

6768
return next();
6869
}

0 commit comments

Comments
 (0)