-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathconsole.ts
44 lines (37 loc) · 1.42 KB
/
console.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-types */
import type { ConsoleLevel, HandlerDataConsole } from '@sentry/types';
import { CONSOLE_LEVELS, originalConsoleMethods } from '../logger';
import { fill } from '../object';
import { GLOBAL_OBJ } from '../worldwide';
import { addHandler, maybeInstrument, triggerHandlers } from './handlers';
/**
* Add an instrumentation handler for when a console.xxx method is called.
*
* Use at your own risk, this might break without changelog notice, only used internally.
* @hidden
*/
export function addConsoleInstrumentationHandler(handler: (data: HandlerDataConsole) => void): void {
const type = 'console';
addHandler(type, handler);
maybeInstrument(type, instrumentConsole);
}
function instrumentConsole(): void {
if (!('console' in GLOBAL_OBJ)) {
return;
}
CONSOLE_LEVELS.forEach(function (level: ConsoleLevel): void {
if (!(level in GLOBAL_OBJ.console)) {
return;
}
fill(GLOBAL_OBJ.console, level, function (originalConsoleMethod: () => any): Function {
originalConsoleMethods[level] = originalConsoleMethod;
return function (...args: any[]): void {
const handlerData: HandlerDataConsole = { args, level };
triggerHandlers('console', handlerData);
const log = originalConsoleMethods[level];
log && log.apply(GLOBAL_OBJ.console, args);
};
});
});
}