1+ import logging
2+ import sys
3+
14from ..assignment import AssignmentConfig
2- from ..cohort .cohort_sync_config import CohortSyncConfig , DEFAULT_COHORT_SYNC_URL , EU_COHORT_SYNC_URL
5+ from ..cohort .cohort_sync_config import (
6+ CohortSyncConfig ,
7+ DEFAULT_COHORT_SYNC_URL ,
8+ EU_COHORT_SYNC_URL ,
9+ )
310from ..server_zone import ServerZone
411
5- DEFAULT_SERVER_URL = ' https://api.lab.amplitude.com'
6- EU_SERVER_URL = ' https://flag.lab.eu.amplitude.com'
12+ DEFAULT_SERVER_URL = " https://api.lab.amplitude.com"
13+ EU_SERVER_URL = " https://flag.lab.eu.amplitude.com"
714
8- DEFAULT_STREAM_URL = ' https://stream.lab.amplitude.com'
9- EU_STREAM_SERVER_URL = ' https://stream.lab.eu.amplitude.com'
15+ DEFAULT_STREAM_URL = " https://stream.lab.amplitude.com"
16+ EU_STREAM_SERVER_URL = " https://stream.lab.eu.amplitude.com"
1017
1118
1219class LocalEvaluationConfig :
1320 """Experiment Local Client Configuration"""
1421
15- def __init__ (self , debug : bool = False ,
16- server_url : str = DEFAULT_SERVER_URL ,
17- server_zone : ServerZone = ServerZone .US ,
18- flag_config_polling_interval_millis : int = 30000 ,
19- flag_config_poller_request_timeout_millis : int = 10000 ,
20- stream_updates : bool = False ,
21- stream_server_url : str = DEFAULT_STREAM_URL ,
22- stream_flag_conn_timeout : int = 1500 ,
23- assignment_config : AssignmentConfig = None ,
24- cohort_sync_config : CohortSyncConfig = None ):
22+ def __init__ (
23+ self ,
24+ debug : bool = False ,
25+ server_url : str = DEFAULT_SERVER_URL ,
26+ server_zone : ServerZone = ServerZone .US ,
27+ flag_config_polling_interval_millis : int = 30000 ,
28+ flag_config_poller_request_timeout_millis : int = 10000 ,
29+ stream_updates : bool = False ,
30+ stream_server_url : str = DEFAULT_STREAM_URL ,
31+ stream_flag_conn_timeout : int = 1500 ,
32+ assignment_config : AssignmentConfig = None ,
33+ cohort_sync_config : CohortSyncConfig = None ,
34+ logger : logging .Logger = None ,
35+ ):
2536 """
2637 Initialize a config
2738 Parameters:
@@ -34,6 +45,8 @@ def __init__(self, debug: bool = False,
3445 fetching flag configurations.
3546 assignment_config (AssignmentConfig): The assignment configuration.
3647 cohort_sync_config (CohortSyncConfig): The cohort sync configuration.
48+ logger (logging.Logger): Optional logger instance. If provided, this logger will be used instead of
49+ creating a new one. The debug flag will still be applied to set the log level.
3750
3851 Returns:
3952 The config object
@@ -44,16 +57,35 @@ def __init__(self, debug: bool = False,
4457 self .cohort_sync_config = cohort_sync_config
4558 if server_url == DEFAULT_SERVER_URL and server_zone == ServerZone .EU :
4659 self .server_url = EU_SERVER_URL
47- if (cohort_sync_config is not None and
48- cohort_sync_config .cohort_server_url == DEFAULT_COHORT_SYNC_URL ):
60+ if (
61+ cohort_sync_config is not None
62+ and cohort_sync_config .cohort_server_url == DEFAULT_COHORT_SYNC_URL
63+ ):
4964 self .cohort_sync_config .cohort_server_url = EU_COHORT_SYNC_URL
5065
5166 self .stream_server_url = stream_server_url
5267 if stream_server_url == DEFAULT_SERVER_URL and server_zone == ServerZone .EU :
5368 self .stream_server_url = EU_STREAM_SERVER_URL
5469
5570 self .flag_config_polling_interval_millis = flag_config_polling_interval_millis
56- self .flag_config_poller_request_timeout_millis = flag_config_poller_request_timeout_millis
71+ self .flag_config_poller_request_timeout_millis = (
72+ flag_config_poller_request_timeout_millis
73+ )
5774 self .stream_updates = stream_updates
5875 self .stream_flag_conn_timeout = stream_flag_conn_timeout
5976 self .assignment_config = assignment_config
77+
78+ # Set up logger: use provided logger or create default one
79+ if logger is None :
80+ self .logger = logging .getLogger ("Amplitude" )
81+ # Only add handler if logger doesn't already have one
82+ if not self .logger .handlers :
83+ handler = logging .StreamHandler (sys .stderr )
84+ self .logger .addHandler (handler )
85+ else :
86+ self .logger = logger
87+
88+ # Set log level: DEBUG if debug=True, otherwise WARNING
89+ # This applies to both provided loggers and the default logger
90+ log_level = logging .DEBUG if self .debug else logging .WARNING
91+ self .logger .setLevel (log_level )
0 commit comments