1
- use std:: { cell:: RefCell , sync:: Arc , time:: Duration } ;
1
+ use std:: { cell:: RefCell , str :: FromStr , sync:: Arc , time:: Duration } ;
2
2
3
3
use eppo_core:: {
4
4
configuration_fetcher:: { ConfigurationFetcher , ConfigurationFetcherConfig } ,
@@ -19,6 +19,7 @@ pub struct Config {
19
19
base_url : String ,
20
20
poll_interval : Option < Duration > ,
21
21
poll_jitter : Duration ,
22
+ log_level : Option < log:: LevelFilter > ,
22
23
}
23
24
24
25
impl TryConvert for Config {
@@ -29,11 +30,22 @@ impl TryConvert for Config {
29
30
let poll_interval_seconds =
30
31
Option :: < u64 > :: try_convert ( val. funcall ( "poll_interval_seconds" , ( ) ) ?) ?;
31
32
let poll_jitter_seconds = u64:: try_convert ( val. funcall ( "poll_jitter_seconds" , ( ) ) ?) ?;
33
+
34
+ let log_level = {
35
+ let s = Option :: < String > :: try_convert ( val. funcall ( "log_level" , ( ) ) ?) ?;
36
+ s. map ( |s| {
37
+ log:: LevelFilter :: from_str ( & s)
38
+ . map_err ( |err| Error :: new ( exception:: runtime_error ( ) , err. to_string ( ) ) )
39
+ } )
40
+ . transpose ( ) ?
41
+ } ;
42
+
32
43
Ok ( Config {
33
44
api_key,
34
45
base_url,
35
46
poll_interval : poll_interval_seconds. map ( Duration :: from_secs) ,
36
47
poll_jitter : Duration :: from_secs ( poll_jitter_seconds) ,
48
+ log_level,
37
49
} )
38
50
}
39
51
}
@@ -52,6 +64,23 @@ pub struct Client {
52
64
53
65
impl Client {
54
66
pub fn new ( config : Config ) -> Client {
67
+ // Initialize logger
68
+ {
69
+ let mut builder = env_logger:: Builder :: from_env (
70
+ env_logger:: Env :: new ( )
71
+ . filter_or ( "EPPO_LOG" , "eppo=info" )
72
+ . write_style ( "EPPO_LOG_STYLE" ) ,
73
+ ) ;
74
+
75
+ if let Some ( log_level) = config. log_level {
76
+ builder. filter_module ( "eppo" , log_level) ;
77
+ }
78
+
79
+ // Logger can only be set once, so we ignore the initialization error here if client is
80
+ // re-initialized.
81
+ let _ = builder. try_init ( ) ;
82
+ } ;
83
+
55
84
let configuration_store = Arc :: new ( ConfigurationStore :: new ( ) ) ;
56
85
57
86
let poller_thread = if let Some ( poll_interval) = config. poll_interval {
0 commit comments