@@ -21,6 +21,8 @@ import { version as PACKAGE_VERSION } from '../package.json';
2121
2222import { Defaults , ExperimentConfig } from './config' ;
2323import { IntegrationManager } from './integration/manager' ;
24+ import { AmpLogger } from './logger/ampLogger' ;
25+ import { ConsoleLogger } from './logger/consoleLogger' ;
2426import {
2527 getFlagStorage ,
2628 getVariantStorage ,
@@ -33,6 +35,7 @@ import { FetchHttpClient, WrapperClient } from './transport/http';
3335import { exposureEvent } from './types/analytics' ;
3436import { Client , FetchOptions } from './types/client' ;
3537import { Exposure , ExposureTrackingProvider } from './types/exposure' ;
38+ import { LogLevel } from './types/logger' ;
3639import { ExperimentPlugin , IntegrationPlugin } from './types/plugin' ;
3740import { ExperimentUserProvider } from './types/provider' ;
3841import { isFallback , Source , VariantSource } from './types/source' ;
@@ -75,6 +78,7 @@ const euFlagsServerUrl = 'https://flag.lab.eu.amplitude.com';
7578export class ExperimentClient implements Client {
7679 private readonly apiKey : string ;
7780 private readonly config : ExperimentConfig ;
81+ private readonly logger : AmpLogger ;
7882 private readonly variants : LoadStoreCache < Variant > ;
7983 private readonly flags : LoadStoreCache < EvaluationFlag > ;
8084 private readonly flagApi : FlagApi ;
@@ -128,6 +132,10 @@ export class ExperimentClient implements Client {
128132 : config . flagConfigPollingIntervalMillis ??
129133 Defaults . flagConfigPollingIntervalMillis ,
130134 } ;
135+ this . logger = new AmpLogger (
136+ this . config . loggerProvider || new ConsoleLogger ( ) ,
137+ ExperimentClient . getLogLevel ( config ) ,
138+ ) ;
131139 const internalInstanceName = this . config ?. [ 'internalInstanceNameSuffix' ] ;
132140 this . isWebExperiment = internalInstanceName === 'web' ;
133141 this . poller = new Poller (
@@ -297,12 +305,10 @@ export class ExperimentClient implements Client {
297305 }
298306
299307 // Otherwise, handle errors silently as before
300- if ( this . config . debug ) {
301- if ( e instanceof TimeoutError ) {
302- console . debug ( e ) ;
303- } else {
304- console . error ( e ) ;
305- }
308+ if ( e instanceof TimeoutError ) {
309+ this . logger . debug ( e ) ;
310+ } else {
311+ this . logger . error ( e ) ;
306312 }
307313 }
308314 return this ;
@@ -332,7 +338,7 @@ export class ExperimentClient implements Client {
332338 if ( this . config . automaticExposureTracking ) {
333339 this . exposureInternal ( key , sourceVariant ) ;
334340 }
335- this . debug (
341+ this . logger . debug (
336342 `[Experiment] variant for ${ key } is ${
337343 sourceVariant . variant ?. key || sourceVariant . variant ?. value
338344 } `,
@@ -697,7 +703,7 @@ export class ExperimentClient implements Client {
697703 throw Error ( 'Experiment API key is empty' ) ;
698704 }
699705
700- this . debug ( `[Experiment] Fetch all: retry=${ retry } ` ) ;
706+ this . logger . debug ( `[Experiment] Fetch all: retry=${ retry } ` ) ;
701707
702708 // Proactively cancel retries if active in order to avoid unnecessary API
703709 // requests. A new failure will restart the retries.
@@ -730,7 +736,7 @@ export class ExperimentClient implements Client {
730736 ) : Promise < Variants > {
731737 user = await this . addContextOrWait ( user ) ;
732738 user = this . cleanUserPropsForFetch ( user ) ;
733- this . debug ( '[Experiment] Fetch variants for user: ' , user ) ;
739+ this . logger . debug ( '[Experiment] Fetch variants for user: ' , user ) ;
734740 const results = await this . evaluationApi . getVariants ( user , {
735741 timeoutMillis : timeoutMillis ,
736742 ...options ,
@@ -739,7 +745,7 @@ export class ExperimentClient implements Client {
739745 for ( const key of Object . keys ( results ) ) {
740746 variants [ key ] = convertEvaluationVariantToVariant ( results [ key ] ) ;
741747 }
742- this . debug ( '[Experiment] Received variants: ' , variants ) ;
748+ this . logger . debug ( '[Experiment] Received variants: ' , variants ) ;
743749 return variants ;
744750 }
745751
@@ -763,7 +769,7 @@ export class ExperimentClient implements Client {
763769 this . flags . putAll ( flags ) ;
764770 } catch ( e ) {
765771 if ( e instanceof TimeoutError ) {
766- this . config . debug && console . debug ( e ) ;
772+ this . logger . debug ( e ) ;
767773 // If throwOnError is configured to true, rethrow timeout errors
768774 if ( this . config . throwOnError ) {
769775 throw e ;
@@ -802,14 +808,14 @@ export class ExperimentClient implements Client {
802808 } catch ( e ) {
803809 // catch localStorage undefined error
804810 }
805- this . debug ( '[Experiment] Stored variants: ' , variants ) ;
811+ this . logger . debug ( '[Experiment] Stored variants: ' , variants ) ;
806812 }
807813
808814 private async startRetries (
809815 user : ExperimentUser ,
810816 options : FetchOptions ,
811817 ) : Promise < void > {
812- this . debug ( '[Experiment] Retry fetch' ) ;
818+ this . logger . debug ( '[Experiment] Retry fetch' ) ;
813819 this . retriesBackoff = new Backoff (
814820 fetchBackoffAttempts ,
815821 fetchBackoffMinMillis ,
@@ -938,11 +944,13 @@ export class ExperimentClient implements Client {
938944 }
939945 }
940946
941- // eslint-disable-next-line @typescript-eslint/no-explicit-any
942- private debug ( message ?: any , ... optionalParams : any [ ] ) : void {
943- if ( this . config . debug ) {
944- console . debug ( message , ... optionalParams ) ;
947+ private static getLogLevel ( config : ExperimentConfig ) : LogLevel {
948+ // Backwards compatibility: if debug flag is set to true, use Debug level
949+ if ( config . debug === true ) {
950+ return LogLevel . Debug ;
945951 }
952+ // Otherwise use the configured logLevel or default to Error
953+ return config . logLevel ?? LogLevel . Error ;
946954 }
947955
948956 private shouldRetryFetch ( e : Error ) : boolean {
0 commit comments