Skip to content

ref: Use consistent console instrumentation #8879

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
Aug 29, 2023
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
77 changes: 41 additions & 36 deletions packages/integrations/src/captureconsole.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { EventProcessor, Hub, Integration } from '@sentry/types';
import { CONSOLE_LEVELS, fill, GLOBAL_OBJ, safeJoin, severityLevelFromString } from '@sentry/utils';
import {
addInstrumentationHandler,
CONSOLE_LEVELS,
GLOBAL_OBJ,
safeJoin,
severityLevelFromString,
} from '@sentry/utils';

/** Send Console API calls as Sentry Events */
export class CaptureConsole implements Integration {
Expand Down Expand Up @@ -34,46 +40,45 @@ export class CaptureConsole implements Integration {
return;
}

this._levels.forEach((level: string) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
if (!(level in (GLOBAL_OBJ as any).console)) {
const levels = this._levels;

addInstrumentationHandler('console', ({ args, level }: { args: unknown[]; level: string }) => {
if (!levels.includes(level)) {
return;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
fill((GLOBAL_OBJ as any).console, level, (originalConsoleMethod: () => any) => (...args: any[]): void => {
const hub = getCurrentHub();

if (hub.getIntegration(CaptureConsole)) {
hub.withScope(scope => {
scope.setLevel(severityLevelFromString(level));
scope.setExtra('arguments', args);
scope.addEventProcessor(event => {
event.logger = 'console';
return event;
});
const hub = getCurrentHub();

let message = safeJoin(args, ' ');
const error = args.find(arg => arg instanceof Error);
if (level === 'assert') {
if (args[0] === false) {
message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`;
scope.setExtra('arguments', args.slice(1));
hub.captureMessage(message);
}
} else if (level === 'error' && error) {
hub.captureException(error);
} else {
hub.captureMessage(message);
}
});
}
if (!hub.getIntegration(CaptureConsole)) {
return;
}

// this fails for some browsers. :(
if (originalConsoleMethod) {
originalConsoleMethod.apply(GLOBAL_OBJ.console, args);
}
});
consoleHandler(hub, args, level);
});
}
}

function consoleHandler(hub: Hub, args: unknown[], level: string): void {
hub.withScope(scope => {
scope.setLevel(severityLevelFromString(level));
scope.setExtra('arguments', args);
scope.addEventProcessor(event => {
event.logger = 'console';
return event;
});

let message = safeJoin(args, ' ');
const error = args.find(arg => arg instanceof Error);
if (level === 'assert') {
if (args[0] === false) {
message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`;
scope.setExtra('arguments', args.slice(1));
hub.captureMessage(message);
}
} else if (level === 'error' && error) {
hub.captureException(error);
} else {
hub.captureMessage(message);
}
});
}
Loading