@@ -27,6 +27,50 @@ export interface SentryWrappedXMLHttpRequest extends XMLHttpRequest {
2727 } ;
2828}
2929
30+ /**
31+ * Wrapper function that'll be used for every console level
32+ */
33+ function consoleWrapper ( originalConsole : ExtensibleConsole ) : any {
34+ return function ( level : string ) : any {
35+ if ( ! ( level in global . console ) ) {
36+ return ;
37+ }
38+
39+ fill ( originalConsole , level , function ( originalConsoleLevel : ( ) => any ) : any {
40+ return function ( ...args : any [ ] ) : any {
41+ const breadcrumbData = {
42+ category : 'console' ,
43+ data : {
44+ extra : {
45+ arguments : args . slice ( 1 ) ,
46+ } ,
47+ logger : 'console' ,
48+ } ,
49+ level : Severity . fromString ( level ) ,
50+ message : safeJoin ( args , ' ' ) ,
51+ } ;
52+
53+ if ( level === 'assert' ) {
54+ if ( args [ 0 ] === false ) {
55+ breadcrumbData . message = `Assertion failed: ${ safeJoin ( args . slice ( 1 ) , ' ' ) || 'console.assert' } ` ;
56+ breadcrumbData . data . extra . arguments = args . slice ( 1 ) ;
57+ }
58+ }
59+
60+ getCurrentHub ( ) . addBreadcrumb ( breadcrumbData , {
61+ input : args ,
62+ level,
63+ } ) ;
64+
65+ // this fails for some browsers. :(
66+ if ( originalConsoleLevel ) {
67+ originalConsoleLevel . apply ( originalConsole , args ) ;
68+ }
69+ } ;
70+ } ) ;
71+ } ;
72+ }
73+
3074/** JSDoc */
3175function addSentryBreadcrumb ( serializedData : string ) : void {
3276 // There's always something that can go wrong with deserialization...
@@ -130,48 +174,8 @@ export class Breadcrumbs implements Integration {
130174 if ( ! ( 'console' in global ) ) {
131175 return ;
132176 }
133-
134177 const originalConsole = global . console as ExtensibleConsole ;
135-
136- [ 'debug' , 'info' , 'warn' , 'error' , 'log' ] . forEach ( level => {
137- if ( ! ( level in console ) ) {
138- return ;
139- }
140-
141- // tslint:disable-next-line
142- const originalConsoleLevel = originalConsole [ level ] ;
143-
144- ( global . console as ExtensibleConsole ) [ level ] = function ( ...args : any [ ] ) : void {
145- const breadcrumbData = {
146- category : 'console' ,
147- data : {
148- extra : {
149- arguments : args . slice ( 1 ) ,
150- } ,
151- logger : 'console' ,
152- } ,
153- level : Severity . fromString ( level ) ,
154- message : safeJoin ( args , ' ' ) ,
155- } ;
156-
157- if ( level === 'assert' ) {
158- if ( args [ 0 ] === false ) {
159- breadcrumbData . message = `Assertion failed: ${ safeJoin ( args . slice ( 1 ) , ' ' ) || 'console.assert' } ` ;
160- breadcrumbData . data . extra . arguments = args . slice ( 1 ) ;
161- }
162- }
163-
164- getCurrentHub ( ) . addBreadcrumb ( breadcrumbData , {
165- input : args ,
166- level,
167- } ) ;
168-
169- // this fails for some browsers. :(
170- if ( originalConsoleLevel ) {
171- Function . prototype . apply . call ( originalConsoleLevel , originalConsole , args ) ;
172- }
173- } ;
174- } ) ;
178+ [ 'debug' , 'info' , 'warn' , 'error' , 'log' ] . forEach ( consoleWrapper ( originalConsole ) ) ;
175179 }
176180
177181 /** JSDoc */
0 commit comments