Skip to content

Commit

Permalink
ref: Use consistent console instrumentation (#8879)
Browse files Browse the repository at this point in the history
While looking into logger issues, I noticed that we fill console.xxx
multiple times. This PR changes that so that we use the console
instrumentation from utils in all cases.
  • Loading branch information
mydea committed Aug 29, 2023
1 parent 7ed50ed commit ff0da3e
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 196 deletions.
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);
}
});
}

0 comments on commit ff0da3e

Please sign in to comment.