Skip to content

Commit f8aeeea

Browse files
authored
ref(utils): Improve logging of objects in logger methods (#4663)
When a variable is used in a template string, its `.toString()` method is called, and most non-native objects, when stringified with `.toString()`, come out as `"[object Object]"`. In our `logger`, we use a template string to concatenate all of the arguments we're given. As a result, users can get error messages like: ``` Sentry Logger [Error]: Error while sending event: [object Object] ``` in cases where the error is not an actual `Error`, which is obviously not very helpful. The underlying `console` methods upon which our `logger` methods rely can generally do better, though, and will attempt to print the actual contents of any object they're given. This changes the `logger` methods so that they pass their arguments on to the `console` methods intact, rather than as a pre-concatenated string, in order to be able to take advantage of the improved logging of objects. It also changes all instances of "error while" logs like the one above to pass their error objects separately rather than as part of a template string, in order to take advantage of this change. Note: There are almost certainly other places in the repo where we log objects which could benefit from this change. In the interests of time, however, this PR is restricted to the ones which brought this issue to light, and others can be solved in future PRs as necessary.
1 parent 3827821 commit f8aeeea

File tree

5 files changed

+9
-10
lines changed

5 files changed

+9
-10
lines changed

packages/core/src/basebackend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export abstract class BaseBackend<O extends Options> implements Backend {
9393
public sendEvent(event: Event): void {
9494
void this._transport.sendEvent(event).then(null, reason => {
9595
if (isDebugBuild()) {
96-
logger.error(`Error while sending event: ${reason}`);
96+
logger.error(`Error while sending event:`, reason);
9797
}
9898
});
9999
}
@@ -111,7 +111,7 @@ export abstract class BaseBackend<O extends Options> implements Backend {
111111

112112
void this._transport.sendSession(session).then(null, reason => {
113113
if (isDebugBuild()) {
114-
logger.error(`Error while sending session: ${reason}`);
114+
logger.error(`Error while sending session:`, reason);
115115
}
116116
});
117117
}

packages/hub/src/sessionflusher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class SessionFlusher implements SessionFlusherLike {
3939
return;
4040
}
4141
void this._transport.sendSession(sessionAggregates).then(null, reason => {
42-
logger.error(`Error while sending session: ${reason}`);
42+
logger.error(`Error while sending session:`, reason);
4343
});
4444
}
4545

packages/nextjs/src/utils/withSentry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,6 @@ async function finishSentryProcessing(res: AugmentedNextApiResponse): Promise<vo
188188
await flush(2000);
189189
logger.log('Done flushing events');
190190
} catch (e) {
191-
logger.log(`Error while flushing events:\n${e}`);
191+
logger.log(`Error while flushing events:\n`, e);
192192
}
193193
}

packages/utils/src/instrument.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ function triggerHandlers(type: InstrumentHandlerType, data: any): void {
9696
} catch (e) {
9797
if (isDebugBuild()) {
9898
logger.error(
99-
`Error while triggering instrumentation handler.\nType: ${type}\nName: ${getFunctionName(
100-
handler,
101-
)}\nError: ${e}`,
99+
`Error while triggering instrumentation handler.\nType: ${type}\nName: ${getFunctionName(handler)}\nError:`,
100+
e,
102101
);
103102
}
104103
}

packages/utils/src/logger.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Logger {
7979
return;
8080
}
8181
consoleSandbox(() => {
82-
global.console.log(`${PREFIX}[Log]: ${args.join(' ')}`);
82+
global.console.log(`${PREFIX}[Log]:`, ...args);
8383
});
8484
}
8585

@@ -89,7 +89,7 @@ class Logger {
8989
return;
9090
}
9191
consoleSandbox(() => {
92-
global.console.warn(`${PREFIX}[Warn]: ${args.join(' ')}`);
92+
global.console.warn(`${PREFIX}[Warn]:`, ...args);
9393
});
9494
}
9595

@@ -99,7 +99,7 @@ class Logger {
9999
return;
100100
}
101101
consoleSandbox(() => {
102-
global.console.error(`${PREFIX}[Error]: ${args.join(' ')}`);
102+
global.console.error(`${PREFIX}[Error]:`, ...args);
103103
});
104104
}
105105
}

0 commit comments

Comments
 (0)