@@ -12,9 +12,9 @@ import {
1212import {
1313 dateTimestampInSeconds ,
1414 getEventDescription ,
15+ Logger ,
1516 isPlainObject ,
1617 isPrimitive ,
17- logger ,
1818 normalize ,
1919 truncate ,
2020 uuid4 ,
@@ -67,6 +67,9 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
6767 /** Options passed to the SDK. */
6868 public readonly options : O ;
6969
70+ /** Logger used for debug mode */
71+ public readonly logger = new Logger ( 'Client' ) ;
72+
7073 /** The client Dsn, if specified in options. Without this Dsn, the SDK will be disabled. */
7174 public readonly dsn ?: Dsn ;
7275
@@ -88,10 +91,14 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
8891 * @param options Options for the client.
8992 */
9093 protected constructor ( options : O ) {
91- this . options = options ;
94+ this . options = options ?? { } ;
95+
96+ if ( this . options . dsn ) {
97+ this . dsn = new Dsn ( this . options . dsn ) ;
98+ }
9299
93- if ( options . dsn ) {
94- this . dsn = new Dsn ( options . dsn ) ;
100+ if ( this . options . debug ) {
101+ this . logger . enabled = true ;
95102 }
96103
97104 this . _transport = this . _setupTransport ( ) ;
@@ -155,7 +162,7 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
155162 */
156163 public captureSession ( session : Session ) : void {
157164 if ( ! session . release ) {
158- logger . warn ( 'Discarded session because of missing release' ) ;
165+ this . logger . warn ( 'Discarded session because of missing release' ) ;
159166 } else {
160167 this . _sendSession ( session ) ;
161168 // After sending, we set init false to inidcate it's not the first occurence
@@ -206,7 +213,7 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
206213 return integrations . reduce ( ( integrationsIndex : Record < string , Integration > , integration ) => {
207214 integrationsIndex [ integration . name ] = integration ;
208215 integration . install ( this ) ;
209- logger . log ( `Integration installed: ${ integration . name } ` ) ;
216+ this . logger . log ( `Integration installed: ${ integration . name } ` ) ;
210217 return integrationsIndex ;
211218 } , { } ) ;
212219 }
@@ -217,7 +224,7 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
217224 // TODO: Do we need generic here?
218225 protected _sendRequest < T > ( request : TransportRequest < T > ) : void {
219226 this . _transport . sendRequest ( request ) . then ( null , reason => {
220- logger . error ( `Failed sending request: ${ reason } ` ) ;
227+ this . logger . error ( `Failed sending request: ${ reason } ` ) ;
221228 } ) ;
222229 }
223230
@@ -431,7 +438,7 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
431438 // eslint-disable-next-line complexity
432439 protected _processEvent ( event : SentryEvent , captureContext : CaptureContext ) : SentryEvent | null {
433440 if ( this . options . enabled === false ) {
434- logger . error ( 'SDK not enabled, will not send event.' ) ;
441+ this . logger . error ( 'SDK not enabled, will not send event.' ) ;
435442 return null ;
436443 }
437444
@@ -440,7 +447,7 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
440447 // 0.0 === 0% events are sent
441448 // Sampling for transaction happens somewhere else
442449 if ( ! isTransaction && typeof this . options . sampleRate === 'number' && Math . random ( ) > this . options . sampleRate ) {
443- logger . error (
450+ this . logger . error (
444451 `Discarding event because it's not included in the random sample (sampling rate = ${ this . options . sampleRate } )` ,
445452 ) ;
446453 return null ;
@@ -466,23 +473,23 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
466473
467474 processedEvent = scope . applyToEvent ( processedEvent , captureContext . hint ) ;
468475 if ( processedEvent === null ) {
469- logger . error ( 'A scope event processor returned null, will not send event.' ) ;
476+ this . logger . error ( 'A scope event processor returned null, will not send event.' ) ;
470477 return null ;
471478 }
472479
473480 for ( const processor of this . _eventProcessors ) {
474481 if ( typeof processor === 'function' ) {
475482 const nextEvent = processor ( processedEvent , captureContext . hint ) ;
476483 if ( nextEvent === null ) {
477- logger . error ( 'A client event processor returned null, will not send event.' ) ;
484+ this . logger . error ( 'A client event processor returned null, will not send event.' ) ;
478485 return null ;
479486 }
480487 processedEvent = nextEvent ;
481488 }
482489 }
483490
484491 if ( processedEvent === null ) {
485- logger . error ( 'A scope event processor returned null, will not send event.' ) ;
492+ this . logger . error ( 'A scope event processor returned null, will not send event.' ) ;
486493 return null ;
487494 }
488495
@@ -499,12 +506,12 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
499506 processedEvent = this . options . beforeSend ( processedEvent as SentryEvent , captureContext ?. hint ) ;
500507
501508 if ( ! ( isPlainObject ( processedEvent ) || processedEvent === null ) ) {
502- logger . error ( '`beforeSend` method has to return `null` or a valid event.' ) ;
509+ this . logger . error ( '`beforeSend` method has to return `null` or a valid event.' ) ;
503510 return null ;
504511 }
505512
506513 if ( processedEvent === null ) {
507- logger . error ( '`beforeSend` returned `null`, will not send event.' ) ;
514+ this . logger . error ( '`beforeSend` returned `null`, will not send event.' ) ;
508515 return null ;
509516 }
510517
@@ -525,7 +532,7 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
525532 originalException : e ,
526533 } ,
527534 } ) ;
528- logger . error (
535+ this . logger . error (
529536 `Event processing pipeline threw an error, original event will not be sent. Details have been sent as a new event.\nReason: ${ e } ` ,
530537 ) ;
531538 return null ;
0 commit comments