Skip to content

Commit 64d1a46

Browse files
authored
feat(astro): Streamline build logs (#17301)
This streamlines astro build logs in two ways: 1. Guard all logs (except the deprecation warning) behind `debug: true` 2. Use the astro logger instead of `console.log` everywhere. This automatically prepends the message with `@sentry/astro`, so no need for our own prefix there - see https://docs.astro.build/en/reference/integrations-reference/#astrointegrationlogger 3. If `sourceMaps: false` is explicitly set by a user, we always show a short warning, and a longer warning with more context when `debug: true`
1 parent ef3cea0 commit 64d1a46

File tree

2 files changed

+72
-56
lines changed

2 files changed

+72
-56
lines changed

packages/astro/src/integration/index.ts

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { consoleSandbox } from '@sentry/core';
21
import { sentryVitePlugin } from '@sentry/vite-plugin';
3-
import type { AstroConfig, AstroIntegration } from 'astro';
2+
import type { AstroConfig, AstroIntegration, AstroIntegrationLogger } from 'astro';
43
import * as fs from 'fs';
54
import * as path from 'path';
65
import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from './snippets';
@@ -36,14 +35,11 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
3635

3736
const otherOptionsKeys = Object.keys(otherOptions);
3837
if (otherOptionsKeys.length > 0) {
39-
consoleSandbox(() => {
40-
// eslint-disable-next-line no-console
41-
console.warn(
42-
`[Sentry] You passed in additional options (${otherOptionsKeys.join(
43-
', ',
44-
)}) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your \`sentry.client.config.(js|ts)\` or \`sentry.server.config.(js|ts)\` files.`,
45-
);
46-
});
38+
logger.warn(
39+
`You passed in additional options (${otherOptionsKeys.join(
40+
', ',
41+
)}) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your \`sentry.client.config.(js|ts)\` or \`sentry.server.config.(js|ts)\` files.`,
42+
);
4743
}
4844

4945
const sdkEnabled = {
@@ -57,7 +53,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
5753

5854
// We don't need to check for AUTH_TOKEN here, because the plugin will pick it up from the env
5955
if (shouldUploadSourcemaps && command !== 'dev') {
60-
const computedSourceMapSettings = getUpdatedSourceMapSettings(config, options);
56+
const computedSourceMapSettings = _getUpdatedSourceMapSettings(config, options, logger);
6157

6258
let updatedFilesToDeleteAfterUpload: string[] | undefined = undefined;
6359

@@ -68,14 +64,12 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
6864
// This also works for adapters, as the source maps are also copied to e.g. the .vercel folder
6965
updatedFilesToDeleteAfterUpload = ['./dist/**/client/**/*.map', './dist/**/server/**/*.map'];
7066

71-
consoleSandbox(() => {
72-
// eslint-disable-next-line no-console
73-
console.log(
74-
`[Sentry] Automatically setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify(
67+
debug &&
68+
logger.info(
69+
`Automatically setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify(
7570
updatedFilesToDeleteAfterUpload,
7671
)}\` to delete generated source maps after they were uploaded to Sentry.`,
7772
);
78-
});
7973
}
8074

8175
updateConfig({
@@ -222,9 +216,10 @@ export type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined;
222216
*
223217
* --> only exported for testing
224218
*/
225-
export function getUpdatedSourceMapSettings(
219+
export function _getUpdatedSourceMapSettings(
226220
astroConfig: AstroConfig,
227-
sentryOptions?: SentryOptions,
221+
sentryOptions: SentryOptions | undefined,
222+
logger: AstroIntegrationLogger,
228223
): { previousUserSourceMapSetting: UserSourceMapSetting; updatedSourceMapSetting: boolean | 'inline' | 'hidden' } {
229224
let previousUserSourceMapSetting: UserSourceMapSetting = undefined;
230225

@@ -234,39 +229,36 @@ export function getUpdatedSourceMapSettings(
234229
let updatedSourceMapSetting = viteSourceMap;
235230

236231
const settingKey = 'vite.build.sourcemap';
232+
const debug = sentryOptions?.debug;
237233

238234
if (viteSourceMap === false) {
239235
previousUserSourceMapSetting = 'disabled';
240236
updatedSourceMapSetting = viteSourceMap;
241237

242-
consoleSandbox(() => {
243-
// eslint-disable-next-line no-console
244-
console.warn(
245-
`[Sentry] Source map generation is currently disabled in your Astro configuration (\`${settingKey}: false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`,
238+
if (debug) {
239+
// Longer debug message with more details
240+
logger.warn(
241+
`Source map generation is currently disabled in your Astro configuration (\`${settingKey}: false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`,
246242
);
247-
});
243+
} else {
244+
logger.warn('Source map generation is disabled in your Astro configuration.');
245+
}
248246
} else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) {
249247
previousUserSourceMapSetting = 'enabled';
250248
updatedSourceMapSetting = viteSourceMap;
251249

252-
if (sentryOptions?.debug) {
253-
consoleSandbox(() => {
254-
// eslint-disable-next-line no-console
255-
console.log(
256-
`[Sentry] We discovered \`${settingKey}\` is set to \`${viteSourceMap.toString()}\`. Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`,
257-
);
258-
});
259-
}
250+
debug &&
251+
logger.info(
252+
`We discovered \`${settingKey}\` is set to \`${viteSourceMap.toString()}\`. Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`,
253+
);
260254
} else {
261255
previousUserSourceMapSetting = 'unset';
262256
updatedSourceMapSetting = 'hidden';
263257

264-
consoleSandbox(() => {
265-
// eslint-disable-next-line no-console
266-
console.log(
267-
`[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`,
258+
debug &&
259+
logger.info(
260+
`Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`,
268261
);
269-
});
270262
}
271263

272264
return { previousUserSourceMapSetting, updatedSourceMapSetting };

packages/astro/test/integration/index.test.ts

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { AstroConfig } from 'astro';
1+
import type { AstroConfig, AstroIntegrationLogger } from 'astro';
22
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
3-
import { getUpdatedSourceMapSettings, sentryAstro } from '../../src/integration';
3+
import { _getUpdatedSourceMapSettings, sentryAstro } from '../../src/integration';
44
import type { SentryOptions } from '../../src/integration/types';
55

66
const sentryVitePluginSpy = vi.fn(() => 'sentryVitePlugin');
@@ -305,21 +305,25 @@ describe('sentryAstro integration', () => {
305305
});
306306

307307
it('injects runtime config into client and server init scripts and warns about deprecation', async () => {
308-
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
309308
const integration = sentryAstro({
310309
environment: 'test',
311310
release: '1.0.0',
312311
dsn: 'https://test.sentry.io/123',
313-
debug: true,
314312
bundleSizeOptimizations: {},
313+
// this also warns when debug is not enabled
315314
});
316315

316+
const logger = {
317+
warn: vi.fn(),
318+
info: vi.fn(),
319+
};
320+
317321
expect(integration.hooks['astro:config:setup']).toBeDefined();
318322
// @ts-expect-error - the hook exists and we only need to pass what we actually use
319-
await integration.hooks['astro:config:setup']({ updateConfig, injectScript, config, logger: { info: vi.fn() } });
323+
await integration.hooks['astro:config:setup']({ updateConfig, injectScript, config, logger });
320324

321-
expect(consoleWarnSpy).toHaveBeenCalledWith(
322-
'[Sentry] You passed in additional options (environment, release, dsn) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.',
325+
expect(logger.warn).toHaveBeenCalledWith(
326+
'You passed in additional options (environment, release, dsn) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.',
323327
);
324328

325329
expect(injectScript).toHaveBeenCalledTimes(2);
@@ -490,18 +494,23 @@ describe('sentryAstro integration', () => {
490494
});
491495
});
492496

493-
describe('getUpdatedSourceMapSettings', () => {
497+
describe('_getUpdatedSourceMapSettings', () => {
494498
let astroConfig: Omit<AstroConfig, 'vite'> & { vite: { build: { sourcemap?: any } } };
495499
let sentryOptions: SentryOptions;
500+
let logger: AstroIntegrationLogger;
496501

497502
beforeEach(() => {
498503
astroConfig = { vite: { build: {} } } as Omit<AstroConfig, 'vite'> & { vite: { build: { sourcemap?: any } } };
499504
sentryOptions = {};
505+
logger = {
506+
info: vi.fn(),
507+
warn: vi.fn(),
508+
} as unknown as AstroIntegrationLogger;
500509
});
501510

502511
it('should keep explicitly disabled source maps disabled', () => {
503512
astroConfig.vite.build.sourcemap = false;
504-
const result = getUpdatedSourceMapSettings(astroConfig, sentryOptions);
513+
const result = _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
505514
expect(result.previousUserSourceMapSetting).toBe('disabled');
506515
expect(result.updatedSourceMapSetting).toBe(false);
507516
});
@@ -515,34 +524,49 @@ describe('getUpdatedSourceMapSettings', () => {
515524

516525
cases.forEach(({ sourcemap, expected }) => {
517526
astroConfig.vite.build.sourcemap = sourcemap;
518-
const result = getUpdatedSourceMapSettings(astroConfig, sentryOptions);
527+
const result = _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
519528
expect(result.previousUserSourceMapSetting).toBe('enabled');
520529
expect(result.updatedSourceMapSetting).toBe(expected);
521530
});
522531
});
523532

524533
it('should enable "hidden" source maps when unset', () => {
525534
astroConfig.vite.build.sourcemap = undefined;
526-
const result = getUpdatedSourceMapSettings(astroConfig, sentryOptions);
535+
const result = _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
527536
expect(result.previousUserSourceMapSetting).toBe('unset');
528537
expect(result.updatedSourceMapSetting).toBe('hidden');
529538
});
530539

531540
it('should log warnings and messages when debug is enabled', () => {
532-
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
533-
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
534-
535541
sentryOptions = { debug: true };
536542

537543
astroConfig.vite.build.sourcemap = false;
538-
getUpdatedSourceMapSettings(astroConfig, sentryOptions);
539-
expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('Source map generation is currently disabled'));
544+
_getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
545+
546+
// eslint-disable-next-line @typescript-eslint/unbound-method
547+
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Source map generation is currently disabled'));
548+
// eslint-disable-next-line @typescript-eslint/unbound-method
549+
expect(logger.warn).toHaveBeenCalledWith(
550+
expect.stringContaining('This setting is either a default setting or was explicitly set in your configuration.'),
551+
);
540552

541553
astroConfig.vite.build.sourcemap = 'hidden';
542-
getUpdatedSourceMapSettings(astroConfig, sentryOptions);
543-
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Sentry will keep this source map setting'));
554+
_getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
555+
// eslint-disable-next-line @typescript-eslint/unbound-method
556+
expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Sentry will keep this source map setting'));
557+
});
544558

545-
consoleWarnSpy.mockRestore();
546-
consoleLogSpy.mockRestore();
559+
it('should show short warnings debug is disabled', () => {
560+
sentryOptions = { debug: false };
561+
562+
astroConfig.vite.build.sourcemap = false;
563+
_getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
564+
// eslint-disable-next-line @typescript-eslint/unbound-method
565+
expect(logger.warn).toHaveBeenCalledWith('Source map generation is disabled in your Astro configuration.');
566+
567+
astroConfig.vite.build.sourcemap = 'hidden';
568+
_getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
569+
// eslint-disable-next-line @typescript-eslint/unbound-method
570+
expect(logger.info).not.toHaveBeenCalled();
547571
});
548572
});

0 commit comments

Comments
 (0)