Skip to content

ref(core): Rewrite logger to avoid side effects #16897

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 1 commit into from
Jul 10, 2025
Merged

Conversation

mydea
Copy link
Member

@mydea mydea commented Jul 10, 2025

This has bugged me often already, and I looked into it again nudged by #16846. Today the logger export from core has a side effect and reads from the global. This is a bit weird...

This PR rewrites this to instead only keep the enabled flag on the carrier. The logger itself is a "normal" method that just looks at the carrier to see if logging is enabled or not.

@mydea mydea self-assigned this Jul 10, 2025
Copy link
Contributor

github-actions bot commented Jul 10, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.86 kB +0.19% +43 B 🔺
@sentry/browser - with treeshaking flags 22.49 kB -0.22% -49 B 🔽
@sentry/browser (incl. Tracing) 39.7 kB +0.07% +27 B 🔺
@sentry/browser (incl. Tracing, Replay) 77.89 kB +0.12% +92 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.73 kB +0.01% +1 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 82.57 kB +0.1% +82 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 94.71 kB +0.12% +107 B 🔺
@sentry/browser (incl. Feedback) 40.54 kB +0.11% +44 B 🔺
@sentry/browser (incl. sendFeedback) 28.53 kB +0.11% +31 B 🔺
@sentry/browser (incl. FeedbackAsync) 33.43 kB +0.13% +42 B 🔺
@sentry/react 25.6 kB -0.01% -2 B 🔽
@sentry/react (incl. Tracing) 41.66 kB +0.01% +3 B 🔺
@sentry/vue 28.27 kB +0.02% +5 B 🔺
@sentry/vue (incl. Tracing) 41.48 kB +0.02% +7 B 🔺
@sentry/svelte 23.87 kB +0.19% +45 B 🔺
CDN Bundle 25.24 kB -0.51% -128 B 🔽
CDN Bundle (incl. Tracing) 39.47 kB -0.41% -161 B 🔽
CDN Bundle (incl. Tracing, Replay) 75.56 kB -0.01% -5 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) 81.02 kB -0.01% -3 B 🔽
CDN Bundle - uncompressed 73.67 kB -0.56% -410 B 🔽
CDN Bundle (incl. Tracing) - uncompressed 117.24 kB -0.39% -458 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed 231.74 kB +0.05% +113 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 244.55 kB +0.05% +113 B 🔺
@sentry/nextjs (client) 43.3 kB - -
@sentry/sveltekit (client) 40.16 kB +0.07% +28 B 🔺
@sentry/node 167.59 kB -0.17% -278 B 🔽
@sentry/node - without tracing 100.3 kB -0.29% -288 B 🔽
@sentry/aws-serverless 128.43 kB -0.21% -266 B 🔽

View base workflow run

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

I wonder if we should rename logger to sentryLogger as well.

This would allow us to export the sentry logging logger from @sentry/core as well.

@mydea mydea marked this pull request as ready for review July 10, 2025 15:09
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Bug: Logger Singleton Issue in Non-DEBUG Builds

In non-DEBUG builds, _getLoggerSettings() incorrectly returns a new { enabled: false } object on each call instead of using the global singleton. This breaks the logger's state management: enable() and disable() become no-ops as they modify a discarded temporary object, and isEnabled() always returns false. This behavior differs from the previous implementation, which correctly maintained the logger's enabled state even when logging was disabled.

packages/core/src/utils/logger.ts#L128-L135

function _getLoggerSettings(): { enabled: boolean } {
if (!DEBUG_BUILD) {
return { enabled: false };
}
return getGlobalSingleton('loggerSettings', () => ({ enabled: false }));
}

packages/core/src/utils/logger.ts#L76-L87

function enable(): void {
_getLoggerSettings().enabled = true;
}
function disable(): void {
_getLoggerSettings().enabled = false;
}
function isEnabled(): boolean {
return _getLoggerSettings().enabled;
}

Fix in CursorFix in Web


Was this report helpful? Give feedback by reacting with 👍 or 👎

@AbhiPrasad
Copy link
Member

Going to merge this in so I can work on #16901

@AbhiPrasad AbhiPrasad merged commit fbf7b1c into develop Jul 10, 2025
332 of 333 checks passed
@AbhiPrasad AbhiPrasad deleted the fn/logger-lazy branch July 10, 2025 15:42
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.

2 participants