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..3259be3f 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 @@ -89,6 +90,21 @@ 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) } + + expect(application.config.logger).to receive(:tagged).with("dotenv").and_call_original + + subject + end + it "watches .env with Spring" do subject expect(Spring.watcher).to include(fixture_path(".env").to_s)