Skip to content

ref(utils): Improve logging of objects in logger methods #4663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2022

Conversation

lobsterkatie
Copy link
Member

@lobsterkatie lobsterkatie commented Mar 2, 2022

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. You can see the difference here:

image

This PR 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.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2022

size-limit report

Path Base Size (196498a) Current Size Change
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.76 KB 19.76 KB +0.05% 🔺
@sentry/browser - ES5 CDN Bundle (minified) 63.33 KB 63.37 KB +0.06% 🔺
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.43 KB 18.42 KB -0.05% 🔽
@sentry/browser - ES6 CDN Bundle (minified) 56.47 KB 56.43 KB -0.07% 🔽
@sentry/browser - Webpack (gzipped + minified) 22.2 KB 22.22 KB +0.08% 🔺
@sentry/browser - Webpack (minified) 76.23 KB 76.29 KB +0.09% 🔺
@sentry/react - Webpack (gzipped + minified) 22.23 KB 22.25 KB +0.08% 🔺
@sentry/nextjs Client - Webpack (gzipped + minified) 46.39 KB 46.4 KB +0.02% 🔺
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 27.25 KB 27.26 KB +0.05% 🔺

@lobsterkatie lobsterkatie requested a review from AbhiPrasad March 2, 2022 01:43
Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks

@lobsterkatie lobsterkatie merged commit f8aeeea into master Mar 2, 2022
@lobsterkatie lobsterkatie deleted the kmclb-log-objects-better branch March 2, 2022 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants