Skip to content

Commit e50b75c

Browse files
authored
feat(browser): Add shim package for logs (#18831)
Adds a shim for the `logger` namespace since we'll soon ship the logs bundle in the loader and want to avoid breaking user apps when they switch back to a bundle that does not include logs closes #18826
1 parent 7401795 commit e50b75c

25 files changed

+372
-10
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
// consoleLoggingIntegration should not actually work, but still not error out
6+
Sentry.init({
7+
dsn: 'https://[email protected]/1337',
8+
sampleRate: 1,
9+
integrations: [Sentry.consoleLoggingIntegration()],
10+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../../utils/fixtures';
3+
4+
sentryTest('exports a shim consoleLoggingIntegration for non-logs bundles', async ({ getLocalTestUrl, page }) => {
5+
const bundle = process.env.PW_BUNDLE;
6+
7+
// Only run this for CDN bundles that do NOT include logs
8+
// Skip minified bundles because DEBUG_BUILD is false and warnings won't appear
9+
if (!bundle?.startsWith('bundle') || bundle.includes('logs') || bundle.includes('min')) {
10+
sentryTest.skip();
11+
}
12+
13+
const consoleMessages: string[] = [];
14+
page.on('console', msg => consoleMessages.push(msg.text()));
15+
16+
let requestCount = 0;
17+
await page.route(/^https:\/\/dsn\.ingest\.sentry\.io\//, route => {
18+
requestCount++;
19+
return route.fulfill({
20+
status: 200,
21+
contentType: 'application/json',
22+
body: JSON.stringify({ id: 'test-id' }),
23+
});
24+
});
25+
26+
const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true });
27+
28+
await page.goto(url);
29+
30+
// Wait a bit to ensure no requests are made
31+
await page.waitForTimeout(500);
32+
33+
expect(requestCount).toBe(0);
34+
expect(consoleMessages).toEqual([
35+
'You are using consoleLoggingIntegration() even though this bundle does not include logs.',
36+
]);
37+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
sampleRate: 1,
8+
});
9+
10+
// These should not actually work, but still not error out
11+
Sentry.logger.trace('test trace');
12+
Sentry.logger.debug('test debug');
13+
Sentry.logger.info('test info');
14+
Sentry.logger.warn('test warn');
15+
Sentry.logger.error('test error');
16+
Sentry.logger.fatal('test fatal');
17+
const testVar = 'test';
18+
Sentry.logger.info(Sentry.logger.fmt`formatted ${testVar}`);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../../utils/fixtures';
3+
4+
sentryTest('exports a shim logger for non-logs bundles', async ({ getLocalTestUrl, page }) => {
5+
const bundle = process.env.PW_BUNDLE;
6+
7+
// Only run this for CDN bundles that do NOT include logs
8+
// Skip minified bundles because DEBUG_BUILD is false and warnings won't appear
9+
if (!bundle?.startsWith('bundle') || bundle.includes('logs') || bundle.includes('min')) {
10+
sentryTest.skip();
11+
}
12+
13+
const consoleMessages: string[] = [];
14+
page.on('console', msg => consoleMessages.push(msg.text()));
15+
16+
let requestCount = 0;
17+
await page.route(/^https:\/\/dsn\.ingest\.sentry\.io\//, route => {
18+
requestCount++;
19+
return route.fulfill({
20+
status: 200,
21+
contentType: 'application/json',
22+
body: JSON.stringify({ id: 'test-id' }),
23+
});
24+
});
25+
26+
const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true });
27+
28+
await page.goto(url);
29+
30+
// Wait a bit to ensure no requests are made
31+
await page.waitForTimeout(500);
32+
33+
expect(requestCount).toBe(0);
34+
35+
expect(consoleMessages).toContain('You are using Sentry.logger.* even though this bundle does not include logs.');
36+
expect(consoleMessages).toContain('You are using Sentry.logger.fmt even though this bundle does not include logs.');
37+
});

packages/browser/src/index.bundle.feedback.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
import { browserTracingIntegrationShim, replayIntegrationShim } from '@sentry-internal/integration-shims';
1+
import {
2+
browserTracingIntegrationShim,
3+
consoleLoggingIntegrationShim,
4+
loggerShim,
5+
replayIntegrationShim,
6+
} from '@sentry-internal/integration-shims';
27
import { feedbackAsyncIntegration } from './feedbackAsync';
38

49
export * from './index.bundle.base';
510

11+
// TODO(v11): Export metricsShim here once we remove metrics from the base bundle.
12+
export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger };
13+
614
export { getFeedback, sendFeedback } from '@sentry-internal/feedback';
715

816
export {

packages/browser/src/index.bundle.replay.feedback.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { browserTracingIntegrationShim } from '@sentry-internal/integration-shims';
1+
import {
2+
browserTracingIntegrationShim,
3+
consoleLoggingIntegrationShim,
4+
loggerShim,
5+
} from '@sentry-internal/integration-shims';
26
import { feedbackAsyncIntegration } from './feedbackAsync';
37

48
export * from './index.bundle.base';
59

10+
// TODO(v11): Export metricsShim here once we remove metrics from the base bundle.
11+
export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger };
12+
613
export { getFeedback, sendFeedback } from '@sentry-internal/feedback';
714

815
export {

packages/browser/src/index.bundle.replay.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { browserTracingIntegrationShim, feedbackIntegrationShim } from '@sentry-internal/integration-shims';
1+
import {
2+
browserTracingIntegrationShim,
3+
consoleLoggingIntegrationShim,
4+
feedbackIntegrationShim,
5+
loggerShim,
6+
} from '@sentry-internal/integration-shims';
27

38
export * from './index.bundle.base';
49

10+
// TODO(v11): Export metricsShim here once we remove metrics from the base bundle.
11+
export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger };
12+
513
export { replayIntegration, getReplay } from '@sentry-internal/replay';
614

715
export {

packages/browser/src/index.bundle.tracing.replay.feedback.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { registerSpanErrorInstrumentation } from '@sentry/core';
2+
import { consoleLoggingIntegrationShim, loggerShim } from '@sentry-internal/integration-shims';
23
import { feedbackAsyncIntegration } from './feedbackAsync';
34

45
registerSpanErrorInstrumentation();
56

67
export * from './index.bundle.base';
78

9+
// TODO(v11): Export metricsShim here once we remove metrics from the base bundle.
10+
export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger };
11+
812
export {
913
getActiveSpan,
1014
getRootSpan,

packages/browser/src/index.bundle.tracing.replay.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { registerSpanErrorInstrumentation } from '@sentry/core';
2-
import { feedbackIntegrationShim } from '@sentry-internal/integration-shims';
2+
import { consoleLoggingIntegrationShim, feedbackIntegrationShim, loggerShim } from '@sentry-internal/integration-shims';
33

44
registerSpanErrorInstrumentation();
55

66
export * from './index.bundle.base';
77

8+
// TODO(v11): Export metricsShim here once we remove metrics from the base bundle.
9+
export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger };
10+
811
export {
912
getActiveSpan,
1013
getRootSpan,

packages/browser/src/index.bundle.tracing.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import { registerSpanErrorInstrumentation } from '@sentry/core';
2-
import { feedbackIntegrationShim, replayIntegrationShim } from '@sentry-internal/integration-shims';
2+
import {
3+
consoleLoggingIntegrationShim,
4+
feedbackIntegrationShim,
5+
loggerShim,
6+
replayIntegrationShim,
7+
} from '@sentry-internal/integration-shims';
38

49
registerSpanErrorInstrumentation();
510

611
export * from './index.bundle.base';
712

13+
// TODO(v11): Export metricsShim here once we remove metrics from the base bundle.
14+
export { consoleLoggingIntegrationShim as consoleLoggingIntegration, loggerShim as logger };
15+
816
export {
917
getActiveSpan,
1018
getRootSpan,

0 commit comments

Comments
 (0)