From d9de363524bd2058a43b4263522af7bc658919bc Mon Sep 17 00:00:00 2001 From: Daniel Pereira Date: Tue, 20 Feb 2024 03:04:45 -0300 Subject: [PATCH 1/3] fix: call `tagged` from an existing tagged logger This way we're ensuring that `dotenv` will work with any tagged logger with the same interface, without ever replacing the original object. --- lib/dotenv/rails.rb | 3 ++- spec/dotenv/rails_spec.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/dotenv/rails.rb b/lib/dotenv/rails.rb index 719b473d..e8e5de0d 100644 --- a/lib/dotenv/rails.rb +++ b/lib/dotenv/rails.rb @@ -90,7 +90,8 @@ def self.load initializer "dotenv", after: :initialize_logger do |app| # Set up a new logger once Rails has initialized the logger and replay logs - new_logger = ActiveSupport::TaggedLogging.new(::Rails.logger).tagged("dotenv") + new_logger = ::Rails.logger + new_logger = new_logger.tagged("dotenv") if new_logger.respond_to?(:tagged) logger.replay new_logger if logger.respond_to?(:replay) self.logger = new_logger end diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index eba6b140..a9ae1e4f 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -89,6 +89,22 @@ context "load" do subject { application.initialize! } + it "use the same rails logger object for simple loggers" do + subject + expect(application.config.dotenv.logger).to equal(::Rails.logger) + end + + it "use a tag when rails is configured to use a tagged logger" do + application.config.logger = ActiveSupport::Logger.new(StringIO.new) + .tap { |logger| logger.formatter = ::Logger::Formatter.new } + .then { |logger| ActiveSupport::TaggedLogging.new(logger) } + allow(application.config.logger).to receive(:tagged).and_call_original + + subject + + expect(application.config.logger).to have_received(:tagged).with("dotenv") + end + it "watches .env with Spring" do subject expect(Spring.watcher).to include(fixture_path(".env").to_s) From 98f1f38ddbe693af22d432829c33eef4f495f58a Mon Sep 17 00:00:00 2001 From: Daniel Pereira Date: Tue, 20 Feb 2024 03:19:01 -0300 Subject: [PATCH 2/3] fix: correctly uninitialize logger between runs --- spec/dotenv/rails_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index a9ae1e4f..80f153bd 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -31,6 +31,7 @@ before do Rails.env = "test" Rails.application = nil + Rails.logger = nil Spring.watcher = Set.new # Responds to #add begin From 16373c5aa7ea282c455a22e6daf5b0001e2915ae Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 26 Feb 2024 12:20:56 -0500 Subject: [PATCH 3/3] Simplify mocking --- spec/dotenv/rails_spec.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index 80f153bd..3259be3f 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -99,11 +99,10 @@ application.config.logger = ActiveSupport::Logger.new(StringIO.new) .tap { |logger| logger.formatter = ::Logger::Formatter.new } .then { |logger| ActiveSupport::TaggedLogging.new(logger) } - allow(application.config.logger).to receive(:tagged).and_call_original - subject + expect(application.config.logger).to receive(:tagged).with("dotenv").and_call_original - expect(application.config.logger).to have_received(:tagged).with("dotenv") + subject end it "watches .env with Spring" do