Skip to content

Commit ff0da3e

Browse files
authored
ref: Use consistent console instrumentation (#8879)
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.
1 parent 7ed50ed commit ff0da3e

File tree

4 files changed

+218
-196
lines changed

4 files changed

+218
-196
lines changed
Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { EventProcessor, Hub, Integration } from '@sentry/types';
2-
import { CONSOLE_LEVELS, fill, GLOBAL_OBJ, safeJoin, severityLevelFromString } from '@sentry/utils';
2+
import {
3+
addInstrumentationHandler,
4+
CONSOLE_LEVELS,
5+
GLOBAL_OBJ,
6+
safeJoin,
7+
severityLevelFromString,
8+
} from '@sentry/utils';
39

410
/** Send Console API calls as Sentry Events */
511
export class CaptureConsole implements Integration {
@@ -34,46 +40,45 @@ export class CaptureConsole implements Integration {
3440
return;
3541
}
3642

37-
this._levels.forEach((level: string) => {
38-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
39-
if (!(level in (GLOBAL_OBJ as any).console)) {
43+
const levels = this._levels;
44+
45+
addInstrumentationHandler('console', ({ args, level }: { args: unknown[]; level: string }) => {
46+
if (!levels.includes(level)) {
4047
return;
4148
}
4249

43-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
44-
fill((GLOBAL_OBJ as any).console, level, (originalConsoleMethod: () => any) => (...args: any[]): void => {
45-
const hub = getCurrentHub();
46-
47-
if (hub.getIntegration(CaptureConsole)) {
48-
hub.withScope(scope => {
49-
scope.setLevel(severityLevelFromString(level));
50-
scope.setExtra('arguments', args);
51-
scope.addEventProcessor(event => {
52-
event.logger = 'console';
53-
return event;
54-
});
50+
const hub = getCurrentHub();
5551

56-
let message = safeJoin(args, ' ');
57-
const error = args.find(arg => arg instanceof Error);
58-
if (level === 'assert') {
59-
if (args[0] === false) {
60-
message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`;
61-
scope.setExtra('arguments', args.slice(1));
62-
hub.captureMessage(message);
63-
}
64-
} else if (level === 'error' && error) {
65-
hub.captureException(error);
66-
} else {
67-
hub.captureMessage(message);
68-
}
69-
});
70-
}
52+
if (!hub.getIntegration(CaptureConsole)) {
53+
return;
54+
}
7155

72-
// this fails for some browsers. :(
73-
if (originalConsoleMethod) {
74-
originalConsoleMethod.apply(GLOBAL_OBJ.console, args);
75-
}
76-
});
56+
consoleHandler(hub, args, level);
7757
});
7858
}
7959
}
60+
61+
function consoleHandler(hub: Hub, args: unknown[], level: string): void {
62+
hub.withScope(scope => {
63+
scope.setLevel(severityLevelFromString(level));
64+
scope.setExtra('arguments', args);
65+
scope.addEventProcessor(event => {
66+
event.logger = 'console';
67+
return event;
68+
});
69+
70+
let message = safeJoin(args, ' ');
71+
const error = args.find(arg => arg instanceof Error);
72+
if (level === 'assert') {
73+
if (args[0] === false) {
74+
message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`;
75+
scope.setExtra('arguments', args.slice(1));
76+
hub.captureMessage(message);
77+
}
78+
} else if (level === 'error' && error) {
79+
hub.captureException(error);
80+
} else {
81+
hub.captureMessage(message);
82+
}
83+
});
84+
}

0 commit comments

Comments
 (0)