From c353a644a444e873935ac9add341f12c57eab1b8 Mon Sep 17 00:00:00 2001 From: Sammy Larbi Date: Wed, 20 Mar 2024 16:37:05 -0500 Subject: [PATCH 1/2] Check that Rails.logger exists before using it Rails logger could be nil, leaving us with a nil value for logger, and unable to load the application due to the assumed presence of a logger. --- lib/ldclient-rb/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ldclient-rb/config.rb b/lib/ldclient-rb/config.rb index 052d4ce0..7c2642be 100644 --- a/lib/ldclient-rb/config.rb +++ b/lib/ldclient-rb/config.rb @@ -458,7 +458,7 @@ def self.default_connect_timeout # @return [Logger] the Rails logger if in Rails, or a default Logger at WARN level otherwise # def self.default_logger - if defined?(Rails) && Rails.respond_to?(:logger) + if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger Rails.logger else log = ::Logger.new($stdout) From 66c4baa176dcebc8b686f63c2c88390a02052876 Mon Sep 17 00:00:00 2001 From: Sammy Larbi Date: Wed, 20 Mar 2024 16:48:52 -0500 Subject: [PATCH 2/2] Add a test case demonstrating the need for this --- spec/config_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 8c6c28ac..fcbd7df3 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -49,6 +49,11 @@ module LaunchDarkly stub_const("Rails", rails) expect(subject.default_logger).to eq :logger end + it "uses logger if Rails logger is nil" do + rails = instance_double("Rails", logger: nil) + stub_const("Rails", rails) + expect(subject.default_logger).to be_an_instance_of Logger + end it "Uses logger if Rails is not available" do expect(subject.default_logger).to be_an_instance_of Logger end