Skip to content

Commit bb42b08

Browse files
author
Julia Eskew
committed
fix: Properly handle an error object sent into logInfo().
At several places in certain MFEs, logInfo() is called with a caught error object. But logInfo() has always assumed that only a string will be passed-in. This situation causes a JSON object to be send to New Relic as the "message" of an info log, which makes the info logs difficult to search. This change adds support for either an info string -or- and error object being sent to logInfo(). The proper "message" is logged either way. TNL-8335
1 parent 417eac9 commit bb42b08

2 files changed

Lines changed: 59 additions & 12 deletions

File tree

src/logging/NewRelicLoggingService.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,32 @@ export default class NewRelicLoggingService {
109109
/**
110110
*
111111
*
112-
* @param {*} message
112+
* @param {*} infoStringOrErrorObject
113113
* @param {*} [customAttributes={}]
114114
* @memberof NewRelicLoggingService
115115
*/
116-
logInfo(message, customAttributes = {}) {
117-
sendPageAction(pageActionNameInfo, message, customAttributes);
116+
logInfo(infoStringOrErrorObject, customAttributes = {}) {
117+
let message = infoStringOrErrorObject;
118+
let customAttrs = customAttributes;
119+
if (typeof infoStringOrErrorObject === 'object' && 'message' in infoStringOrErrorObject) {
120+
/* Caller has passed in an error object to be logged as a page action. */
121+
/* Extract the attributes and the message. */
122+
const infoCustomAttributes = infoStringOrErrorObject.customAttributes || {};
123+
customAttrs = { ...infoCustomAttributes, ...customAttributes };
124+
message = infoStringOrErrorObject.message;
125+
}
126+
sendPageAction(pageActionNameInfo, message, customAttrs);
118127
}
119128

120129
/**
121130
*
122131
*
123-
* @param {*} error
132+
* @param {*} errorStringOrObject
124133
* @param {*} [customAttributes={}]
125134
* @memberof NewRelicLoggingService
126135
*/
127-
logError(error, customAttributes = {}) {
128-
const errorCustomAttributes = error.customAttributes || {};
136+
logError(errorStringOrObject, customAttributes = {}) {
137+
const errorCustomAttributes = errorStringOrObject.customAttributes || {};
129138
let allCustomAttributes = { ...errorCustomAttributes, ...customAttributes };
130139
if (Object.keys(allCustomAttributes).length === 0) {
131140
// noticeError expects undefined if there are no custom attributes.
@@ -137,13 +146,13 @@ export default class NewRelicLoggingService {
137146
Ignored errors are logged via adding a page action.
138147
Other errors are logged via noticeError and count as "JS Errors" for the application.
139148
*/
140-
const errorMessage = typeof error === 'string' ? error : error.message;
149+
const errorMessage = errorStringOrObject.message || (typeof errorStringOrObject === 'string' ? errorStringOrObject : '');
141150
if (this.ignoredErrorRegexes && errorMessage.match(this.ignoredErrorRegexes)) {
142151
/* ignored error */
143152
sendPageAction(pageActionNameIgnoredError, errorMessage, allCustomAttributes);
144153
} else {
145154
/* error! */
146-
sendError(error, allCustomAttributes);
155+
sendError(errorStringOrObject, allCustomAttributes);
147156
}
148157
}
149158
}

src/logging/NewRelicLoggingService.test.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,50 @@ describe('NewRelicLoggingService', () => {
4646
expect(global.newrelic.addPageAction).toHaveBeenCalledWith('INFO', { message });
4747
});
4848

49-
it('passes info parameters properly with custom attributes', () => {
49+
it('handles plain string message properly with custom attributes', () => {
5050
const message = 'Test log';
51-
const customAttrs = { a: 1, b: 'red', c: 3 };
52-
service.logInfo(message, customAttrs);
51+
const attrs = { a: 1, b: 'red', c: 3 };
52+
service.logInfo(message, attrs);
5353
expect(global.newrelic.addPageAction).toHaveBeenCalledWith('INFO', {
54-
message: 'Test log', a: 1, b: 'red', c: 3,
54+
message, ...attrs,
55+
});
56+
});
57+
58+
it('handles plain string message properly with no custom attributes', () => {
59+
const message = 'Test log';
60+
service.logInfo(message);
61+
expect(global.newrelic.addPageAction).toHaveBeenCalledWith('INFO', {
62+
message,
63+
});
64+
});
65+
66+
it('handles error object properly with custom attributes', () => {
67+
const message = 'Test log';
68+
const attrs = { a: 1, b: 'red', c: 3 };
69+
const err = { message, customAttributes: attrs };
70+
service.logInfo(err);
71+
expect(global.newrelic.addPageAction).toHaveBeenCalledWith('INFO', {
72+
message, ...attrs,
73+
});
74+
});
75+
76+
it('handles error object properly with no custom attributes', () => {
77+
const message = 'Test log';
78+
const err = { message };
79+
service.logInfo(err);
80+
expect(global.newrelic.addPageAction).toHaveBeenCalledWith('INFO', {
81+
message,
82+
});
83+
});
84+
85+
it('handles error object properly with custom attributes in object and param', () => {
86+
const message = 'Test log';
87+
const attrsObj = { a: 1, b: 'red', c: 3 };
88+
const attrsParam = { x: 99, y: 'blue', z: 987 };
89+
const err = { message, customAttributes: attrsObj };
90+
service.logInfo(err, attrsParam);
91+
expect(global.newrelic.addPageAction).toHaveBeenCalledWith('INFO', {
92+
message, ...attrsObj, ...attrsParam,
5593
});
5694
});
5795
});

0 commit comments

Comments
 (0)