Skip to content

ref(utils): Use array constant for console levels #4709

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
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/integrations/src/captureconsole.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventProcessor, Hub, Integration } from '@sentry/types';
import { fill, getGlobalObject, safeJoin, severityFromString } from '@sentry/utils';
import { CONSOLE_LEVELS, fill, getGlobalObject, safeJoin, severityFromString } from '@sentry/utils';

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

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

/**
* @inheritDoc
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { WrappedFunction } from '@sentry/types';
import { isDebugBuild } from './env';
import { getGlobalObject } from './global';
import { isInstanceOf, isString } from './is';
import { logger } from './logger';
import { CONSOLE_LEVELS, logger } from './logger';
import { fill } from './object';
import { getFunctionName } from './stacktrace';
import { supportsHistory, supportsNativeFetch } from './supports';
Expand Down Expand Up @@ -110,7 +110,7 @@ function instrumentConsole(): void {
return;
}

['debug', 'info', 'warn', 'error', 'log', 'assert'].forEach(function (level: string): void {
CONSOLE_LEVELS.forEach(function (level: string): void {
if (!(level in global.console)) {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/utils/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const global = getGlobalObject<Window | NodeJS.Global>();
/** Prefix for logging strings */
const PREFIX = 'Sentry Logger ';

export const CONSOLE_LEVELS = ['debug', 'info', 'warn', 'error', 'log', 'assert'];

/** JSDoc */
interface ExtensibleConsole extends Console {
[key: string]: any;
Expand All @@ -24,7 +26,6 @@ interface ExtensibleConsole extends Console {
*/
export function consoleSandbox(callback: () => any): any {
const global = getGlobalObject<Window>();
const levels = ['debug', 'info', 'warn', 'error', 'log', 'assert'];

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

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