Skip to content

Commit e788c9e

Browse files
authored
ref(utils): Use array constant for console levels (#4709)
Currently, the array `['log', 'info', 'warn', 'error', 'debug', 'assert']` appears three times in our code, and because it's an array of strings, it's can't be minified. This PR uses a constant in its place, for a small bundle size win.
1 parent 451bc82 commit e788c9e

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

packages/integrations/src/captureconsole.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventProcessor, Hub, Integration } from '@sentry/types';
2-
import { fill, getGlobalObject, safeJoin, severityFromString } from '@sentry/utils';
2+
import { CONSOLE_LEVELS, fill, getGlobalObject, safeJoin, severityFromString } from '@sentry/utils';
33

44
const global = getGlobalObject<Window | NodeJS.Global>();
55

@@ -18,7 +18,7 @@ export class CaptureConsole implements Integration {
1818
/**
1919
* @inheritDoc
2020
*/
21-
private readonly _levels: string[] = ['log', 'info', 'warn', 'error', 'debug', 'assert'];
21+
private readonly _levels: string[] = CONSOLE_LEVELS;
2222

2323
/**
2424
* @inheritDoc

packages/utils/src/instrument.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { WrappedFunction } from '@sentry/types';
66
import { isDebugBuild } from './env';
77
import { getGlobalObject } from './global';
88
import { isInstanceOf, isString } from './is';
9-
import { logger } from './logger';
9+
import { CONSOLE_LEVELS, logger } from './logger';
1010
import { fill } from './object';
1111
import { getFunctionName } from './stacktrace';
1212
import { supportsHistory, supportsNativeFetch } from './supports';
@@ -110,7 +110,7 @@ function instrumentConsole(): void {
110110
return;
111111
}
112112

113-
['debug', 'info', 'warn', 'error', 'log', 'assert'].forEach(function (level: string): void {
113+
CONSOLE_LEVELS.forEach(function (level: string): void {
114114
if (!(level in global.console)) {
115115
return;
116116
}

packages/utils/src/logger.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const global = getGlobalObject<Window | NodeJS.Global>();
1010
/** Prefix for logging strings */
1111
const PREFIX = 'Sentry Logger ';
1212

13+
export const CONSOLE_LEVELS = ['debug', 'info', 'warn', 'error', 'log', 'assert'];
14+
1315
/** JSDoc */
1416
interface ExtensibleConsole extends Console {
1517
[key: string]: any;
@@ -24,7 +26,6 @@ interface ExtensibleConsole extends Console {
2426
*/
2527
export function consoleSandbox(callback: () => any): any {
2628
const global = getGlobalObject<Window>();
27-
const levels = ['debug', 'info', 'warn', 'error', 'log', 'assert'];
2829

2930
if (!('console' in global)) {
3031
return callback();
@@ -35,7 +36,7 @@ export function consoleSandbox(callback: () => any): any {
3536
const wrappedLevels: { [key: string]: any } = {};
3637

3738
// Restore all wrapped console methods
38-
levels.forEach(level => {
39+
CONSOLE_LEVELS.forEach(level => {
3940
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
4041
if (level in (global as any).console && (originalConsole[level] as WrappedFunction).__sentry_original__) {
4142
wrappedLevels[level] = originalConsole[level] as WrappedFunction;

0 commit comments

Comments
 (0)