11/* eslint-disable @typescript-eslint/no-explicit-any */
22/* eslint-disable @typescript-eslint/ban-types */
33import type { ConsoleLevel , HandlerDataConsole } from '../types-hoist/instrument' ;
4- import type { WrappedFunction } from '../types-hoist/wrappedfunction' ;
54import { CONSOLE_LEVELS , originalConsoleMethods } from '../utils/debug-logger' ;
6- import { fill , markFunctionWrapped } from '../utils/object' ;
5+ import { fill } from '../utils/object' ;
76import { GLOBAL_OBJ } from '../utils/worldwide' ;
87import { addHandler , maybeInstrument , triggerHandlers } from './handlers' ;
98
@@ -29,83 +28,15 @@ function instrumentConsole(): void {
2928 return ;
3029 }
3130
32- if ( typeof process !== 'undefined' && ! ! process . env . LAMBDA_TASK_ROOT ) {
33- // The AWS Lambda runtime replaces console methods AFTER our patch, which overwrites them.
34- patchWithDefineProperty ( level ) ;
35- } else {
36- patchWithFill ( level ) ;
37- }
38- } ) ;
39- }
40-
41- function patchWithFill ( level : ConsoleLevel ) : void {
42- fill ( GLOBAL_OBJ . console , level , function ( originalConsoleMethod : ( ) => any ) : Function {
43- originalConsoleMethods [ level ] = originalConsoleMethod ;
44-
45- return function ( ...args : any [ ] ) : void {
46- triggerHandlers ( 'console' , { args, level } as HandlerDataConsole ) ;
47-
48- const log = originalConsoleMethods [ level ] ;
49- log ?. apply ( GLOBAL_OBJ . console , args ) ;
50- } ;
51- } ) ;
52- }
31+ fill ( GLOBAL_OBJ . console , level , function ( originalConsoleMethod : ( ) => any ) : Function {
32+ originalConsoleMethods [ level ] = originalConsoleMethod ;
5333
54- function patchWithDefineProperty ( level : ConsoleLevel ) : void {
55- const nativeMethod = GLOBAL_OBJ . console [ level ] as ( ...args : unknown [ ] ) => void ;
56- originalConsoleMethods [ level ] = nativeMethod ;
34+ return function ( ...args : any [ ] ) : void {
35+ triggerHandlers ( 'console' , { args, level } as HandlerDataConsole ) ;
5736
58- let consoleDelegate : Function = nativeMethod ;
59- let isExecuting = false ;
60-
61- const wrapper = function ( ...args : any [ ] ) : void {
62- if ( isExecuting ) {
63- // Re-entrant call: a third party captured `wrapper` via the getter and calls it
64- // from inside their replacement (e.g. `const prev = console.log; console.log = (...a) => { prev(...a); }`).
65- // Calling `consoleDelegate` here would recurse, so fall back to the native method.
66- nativeMethod . apply ( GLOBAL_OBJ . console , args ) ;
67- return ;
68- }
69- isExecuting = true ;
70- try {
71- triggerHandlers ( 'console' , { args, level } ) ;
72- consoleDelegate . apply ( GLOBAL_OBJ . console , args ) ;
73- } finally {
74- isExecuting = false ;
75- }
76- } ;
77- markFunctionWrapped ( wrapper as unknown as WrappedFunction , nativeMethod as unknown as WrappedFunction ) ;
78-
79- try {
80- let current : any = wrapper ;
81-
82- Object . defineProperty ( GLOBAL_OBJ . console , level , {
83- configurable : true ,
84- enumerable : true ,
85- get ( ) {
86- return current ;
87- } ,
88- // When `console[level]` is set to a new value, we want to check if it's something not done by us but by e.g. the Lambda runtime.
89- set ( newValue ) {
90- if (
91- typeof newValue === 'function' &&
92- // Ignore if it's set to the wrapper (e.g. by our own patch or consoleSandbox), which would cause an infinite loop.
93- newValue !== wrapper &&
94- // Function is not one of our wrappers (which have __sentry_original__) and not the original (stored in originalConsoleMethods)
95- newValue !== originalConsoleMethods [ level ] &&
96- ! ( newValue as WrappedFunction ) . __sentry_original__
97- ) {
98- // Absorb newly "set" function as the consoleDelegate but keep our wrapper as the active method.
99- consoleDelegate = newValue ;
100- current = wrapper ;
101- } else {
102- // Accept as-is: consoleSandbox restoring, other Sentry wrappers, or non-functions
103- current = newValue ;
104- }
105- } ,
37+ const log = originalConsoleMethods [ level ] ;
38+ log ?. apply ( GLOBAL_OBJ . console , args ) ;
39+ } ;
10640 } ) ;
107- } catch {
108- // In case defineProperty fails (e.g. in older browsers), fall back to fill-style patching
109- patchWithFill ( level ) ;
110- }
41+ } ) ;
11142}
0 commit comments