Skip to content

Commit d2d6d77

Browse files
sameerankrasendubi
andauthored
FF-3593 feat(ruby): allow configuring log level from config (#72)
* Set default log level to info * Fix checksum in cargo.lock * Move logger initialization * Bump ruby version * FF-3593 feat(ruby): Allow overriding log level * FF-3593 docs(ruby): add documentation on logging --------- Co-authored-by: Oleksii Shmalko <[email protected]>
1 parent 4137629 commit d2d6d77

File tree

9 files changed

+1267
-22
lines changed

9 files changed

+1267
-22
lines changed

ruby-sdk/Cargo.lock

Lines changed: 3 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ruby-sdk/Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
eppo-server-sdk (3.2.8)
4+
eppo-server-sdk (3.3.0)
55
rb_sys (~> 0.9.102)
66

77
GEM

ruby-sdk/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ Refer to our [SDK documentation](https://docs.geteppo.com/feature-flags/sdks/rub
77
## Supported Ruby Versions
88
This version of the SDK is compatible with Ruby 3.0.6 and above.
99

10+
## Logging
11+
12+
Ruby SDK uses [`env_logger`](https://docs.rs/env_logger/) for logging.
13+
14+
Starting from version 3.3.0, the log level can be configured via `EPPO_LOG` environment variable using one of the following values:
15+
- `off`
16+
- `error`
17+
- `warn`
18+
- `info` (default)
19+
- `debug`
20+
- `trace`
21+
22+
Alternatively, it can be configured using `log_level` parameter for `EppoClient::Config` constructor:
23+
```ruby
24+
config = EppoClient::Config.new("sdk-key", log_level: "debug")
25+
```
26+
1027
# Contributing
1128

1229
## Testing with local version of `eppo_core`

ruby-sdk/ext/eppo_client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "eppo_client"
33
# TODO: this version and lib/eppo_client/version.rb should be in sync
4-
version = "3.2.8"
4+
version = "3.3.0"
55
edition = "2021"
66
license = "MIT"
77
publish = false

ruby-sdk/ext/eppo_client/src/client.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, sync::Arc, time::Duration};
1+
use std::{cell::RefCell, str::FromStr, sync::Arc, time::Duration};
22

33
use eppo_core::{
44
configuration_fetcher::{ConfigurationFetcher, ConfigurationFetcherConfig},
@@ -19,6 +19,7 @@ pub struct Config {
1919
base_url: String,
2020
poll_interval: Option<Duration>,
2121
poll_jitter: Duration,
22+
log_level: Option<log::LevelFilter>,
2223
}
2324

2425
impl TryConvert for Config {
@@ -29,11 +30,22 @@ impl TryConvert for Config {
2930
let poll_interval_seconds =
3031
Option::<u64>::try_convert(val.funcall("poll_interval_seconds", ())?)?;
3132
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+
3243
Ok(Config {
3344
api_key,
3445
base_url,
3546
poll_interval: poll_interval_seconds.map(Duration::from_secs),
3647
poll_jitter: Duration::from_secs(poll_jitter_seconds),
48+
log_level,
3749
})
3850
}
3951
}
@@ -52,6 +64,23 @@ pub struct Client {
5264

5365
impl Client {
5466
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+
5584
let configuration_store = Arc::new(ConfigurationStore::new());
5685

5786
let poller_thread = if let Some(poll_interval) = config.poll_interval {

ruby-sdk/ext/eppo_client/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ pub(crate) const SDK_METADATA: SdkMetadata = SdkMetadata {
1414

1515
#[magnus::init]
1616
fn init(ruby: &Ruby) -> Result<(), Error> {
17-
env_logger::Builder::from_env(env_logger::Env::new().default_filter_or("eppo=debug")).init();
18-
1917
let eppo_client = ruby.define_module("EppoClient")?;
2018
let core = eppo_client.define_module("Core")?;
2119

ruby-sdk/lib/eppo_client/config.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
module EppoClient
77
# The class for configuring the Eppo client singleton
88
class Config
9-
attr_reader :api_key, :assignment_logger, :base_url, :poll_interval_seconds, :poll_jitter_seconds
9+
attr_reader :api_key, :assignment_logger, :base_url, :poll_interval_seconds, :poll_jitter_seconds, :log_level
1010

11-
def initialize(api_key, assignment_logger: AssignmentLogger.new, base_url: EppoClient::Core::DEFAULT_BASE_URL, poll_interval_seconds: EppoClient::Core::DEFAULT_POLL_INTERVAL_SECONDS, poll_jitter_seconds: EppoClient::Core::DEFAULT_POLL_JITTER_SECONDS, initial_configuration: nil)
11+
def initialize(api_key, assignment_logger: AssignmentLogger.new, base_url: EppoClient::Core::DEFAULT_BASE_URL, poll_interval_seconds: EppoClient::Core::DEFAULT_POLL_INTERVAL_SECONDS, poll_jitter_seconds: EppoClient::Core::DEFAULT_POLL_JITTER_SECONDS, initial_configuration: nil, log_level: nil)
1212
@api_key = api_key
1313
@assignment_logger = assignment_logger
1414
@base_url = base_url
1515
@poll_interval_seconds = poll_interval_seconds
1616
@poll_jitter_seconds = poll_jitter_seconds
17+
@log_level = log_level
1718
end
1819

1920
def validate

ruby-sdk/lib/eppo_client/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
# TODO: this version and ext/eppo_client/Cargo.toml should be in sync
44
module EppoClient
5-
VERSION = "3.2.8"
5+
VERSION = "3.3.0"
66
end

0 commit comments

Comments
 (0)