Skip to content

Commit

Permalink
Merge pull request #489 from kriansa/fix/use-tagged-logger
Browse files Browse the repository at this point in the history
Use tagged logger already configured on Rails
  • Loading branch information
bkeepers committed Feb 26, 2024
2 parents 96b05b0 + 16373c5 commit ccfabc0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/dotenv/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,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
Expand Down
16 changes: 16 additions & 0 deletions spec/dotenv/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
before do
Rails.env = "test"
Rails.application = nil
Rails.logger = nil
Spring.watcher = Set.new # Responds to #add

begin
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ccfabc0

Please sign in to comment.