@@ -18,82 +18,53 @@ export class Console implements Integration {
18
18
* @inheritDoc
19
19
*/
20
20
public setupOnce ( ) : void {
21
- const nativeModule = require ( 'module ' ) ;
22
- fill ( nativeModule , '_load ' , loadWrapper ( nativeModule ) ) ;
23
- // special case: since console is built-in and app- level code won't require() it, do that here
24
- require ( 'console' ) ;
21
+ const consoleModule = require ( 'console ' ) ;
22
+ for ( const level of [ 'debug' , 'info ' , 'warn' , 'error' , 'log' ] ) {
23
+ fill ( consoleModule , level , createConsoleWrapper ( level ) ) ;
24
+ }
25
25
}
26
26
}
27
27
28
- /**
29
- * Wrapper function for internal _load calls within `require`
30
- */
31
- function loadWrapper ( nativeModule : any ) : any {
32
- // We need to use some functional-style currying to pass values around
33
- // as we cannot rely on `bind`, because this has to preserve correct
34
- // context for native calls
35
- return function ( originalLoad : ( ) => any ) : any {
36
- return function ( moduleId : string ) : any {
37
- const originalModule = originalLoad . apply ( nativeModule , arguments ) ;
38
-
39
- if ( moduleId !== 'console' || originalModule . __sentry__ ) {
40
- return originalModule ;
41
- }
42
-
43
- [ 'debug' , 'info' , 'warn' , 'error' , 'log' ] . forEach ( consoleWrapper ( originalModule ) ) ;
44
-
45
- originalModule . __sentry__ = true ;
46
- return originalModule ;
47
- } ;
48
- } ;
49
- }
50
-
51
28
/**
52
29
* Wrapper function that'll be used for every console level
53
30
*/
54
- function consoleWrapper ( originalModule : any ) : any {
55
- return function ( level : string ) : any {
56
- if ( ! ( level in originalModule ) ) {
57
- return ;
58
- }
31
+ function createConsoleWrapper ( level : string ) : ( originalConsoleMethod : ( ) => void ) => void {
32
+ return function consoleWrapper ( originalConsoleMethod : ( ) => void ) : ( ) => void {
33
+ let sentryLevel : Severity ;
59
34
60
- fill ( originalModule , level , function ( originalConsoleLevel : ( ) => any ) : any {
61
- let sentryLevel : Severity ;
35
+ switch ( level ) {
36
+ case 'debug' :
37
+ sentryLevel = Severity . Debug ;
38
+ break ;
39
+ case 'error' :
40
+ sentryLevel = Severity . Error ;
41
+ break ;
42
+ case 'info' :
43
+ sentryLevel = Severity . Info ;
44
+ break ;
45
+ case 'warn' :
46
+ sentryLevel = Severity . Warning ;
47
+ break ;
48
+ default :
49
+ sentryLevel = Severity . Log ;
50
+ }
62
51
63
- switch ( level ) {
64
- case 'debug' :
65
- sentryLevel = Severity . Debug ;
66
- break ;
67
- case 'error' :
68
- sentryLevel = Severity . Error ;
69
- break ;
70
- case 'info' :
71
- sentryLevel = Severity . Info ;
72
- break ;
73
- case 'warn' :
74
- sentryLevel = Severity . Warning ;
75
- break ;
76
- default :
77
- sentryLevel = Severity . Log ;
52
+ return function ( this : typeof console ) : void {
53
+ if ( getCurrentHub ( ) . getIntegration ( Console ) ) {
54
+ getCurrentHub ( ) . addBreadcrumb (
55
+ {
56
+ category : 'console' ,
57
+ level : sentryLevel ,
58
+ message : util . format . apply ( undefined , arguments ) ,
59
+ } ,
60
+ {
61
+ input : [ ...arguments ] ,
62
+ level,
63
+ } ,
64
+ ) ;
78
65
}
79
66
80
- return function ( ) : any {
81
- if ( getCurrentHub ( ) . getIntegration ( Console ) ) {
82
- getCurrentHub ( ) . addBreadcrumb (
83
- {
84
- category : 'console' ,
85
- level : sentryLevel ,
86
- message : util . format . apply ( undefined , arguments ) ,
87
- } ,
88
- {
89
- input : [ ...arguments ] ,
90
- level,
91
- } ,
92
- ) ;
93
- }
94
-
95
- originalConsoleLevel . apply ( originalModule , arguments ) ;
96
- } ;
97
- } ) ;
67
+ originalConsoleMethod . apply ( this , arguments ) ;
68
+ } ;
98
69
} ;
99
70
}
0 commit comments