1 | import { CONSOLE_LEVELS, GLOBAL_OBJ, fill, severityLevelFromString, safeJoin } from '@sentry/utils';
|
2 |
|
3 |
|
4 | class CaptureConsole {
|
5 | |
6 |
|
7 |
|
8 | static __initStatic() {this.id = 'CaptureConsole';}
|
9 |
|
10 | |
11 |
|
12 |
|
13 | __init() {this.name = CaptureConsole.id;}
|
14 |
|
15 | |
16 |
|
17 |
|
18 | __init2() {this._levels = CONSOLE_LEVELS;}
|
19 |
|
20 | |
21 |
|
22 |
|
23 | constructor(options = {}) {CaptureConsole.prototype.__init.call(this);CaptureConsole.prototype.__init2.call(this);
|
24 | if (options.levels) {
|
25 | this._levels = options.levels;
|
26 | }
|
27 | }
|
28 |
|
29 | |
30 |
|
31 |
|
32 | setupOnce(_, getCurrentHub) {
|
33 | if (!('console' in GLOBAL_OBJ)) {
|
34 | return;
|
35 | }
|
36 |
|
37 | this._levels.forEach((level) => {
|
38 |
|
39 | if (!(level in (GLOBAL_OBJ ).console)) {
|
40 | return;
|
41 | }
|
42 |
|
43 |
|
44 | fill((GLOBAL_OBJ ).console, level, (originalConsoleMethod) => (...args) => {
|
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 | });
|
55 |
|
56 | let message = safeJoin(args, ' ');
|
57 | if (level === 'assert') {
|
58 | if (args[0] === false) {
|
59 | message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`;
|
60 | scope.setExtra('arguments', args.slice(1));
|
61 | hub.captureMessage(message);
|
62 | }
|
63 | } else if (level === 'error' && args[0] instanceof Error) {
|
64 | hub.captureException(args[0]);
|
65 | } else {
|
66 | hub.captureMessage(message);
|
67 | }
|
68 | });
|
69 | }
|
70 |
|
71 |
|
72 | if (originalConsoleMethod) {
|
73 | originalConsoleMethod.apply(GLOBAL_OBJ.console, args);
|
74 | }
|
75 | });
|
76 | });
|
77 | }
|
78 | } CaptureConsole.__initStatic();
|
79 |
|
80 | export { CaptureConsole };
|
81 |
|