@sentry/integrations
Version:
Pluggable integrations that can be used to enhance JS SDKs
82 lines (70 loc) • 2.38 kB
JavaScript
import { CONSOLE_LEVELS, GLOBAL_OBJ, fill, severityLevelFromString, safeJoin } from '@sentry/utils';
/** Send Console API calls as Sentry Events */
class CaptureConsole {
/**
* @inheritDoc
*/
static __initStatic() {this.id = 'CaptureConsole';}
/**
* @inheritDoc
*/
__init() {this.name = CaptureConsole.id;}
/**
* @inheritDoc
*/
__init2() {this._levels = CONSOLE_LEVELS;}
/**
* @inheritDoc
*/
constructor(options = {}) {;CaptureConsole.prototype.__init.call(this);CaptureConsole.prototype.__init2.call(this);
if (options.levels) {
this._levels = options.levels;
}
}
/**
* @inheritDoc
*/
setupOnce(_, getCurrentHub) {
if (!('console' in GLOBAL_OBJ)) {
return;
}
this._levels.forEach((level) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
if (!(level in (GLOBAL_OBJ ).console)) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
fill((GLOBAL_OBJ ).console, level, (originalConsoleMethod) => (...args) => {
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;
});
let message = safeJoin(args, ' ');
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' && args[0] instanceof Error) {
hub.captureException(args[0]);
} else {
hub.captureMessage(message);
}
});
}
// this fails for some browsers. :(
if (originalConsoleMethod) {
originalConsoleMethod.apply(GLOBAL_OBJ.console, args);
}
});
});
}
} CaptureConsole.__initStatic();
export { CaptureConsole };
//# sourceMappingURL=captureconsole.js.map