Skip to content

Commit

Permalink
Fix regression with semantic-logger, allow setting custom logger.
Browse files Browse the repository at this point in the history
This partially reverts the change in #489
  • Loading branch information
bkeepers committed Feb 27, 2024
1 parent 87cd07e commit 3e0b183
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
14 changes: 9 additions & 5 deletions lib/dotenv/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def root
::Rails.root || Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd)
end

# Set a new logger and replay logs
def logger=(new_logger)
logger.replay new_logger if logger.is_a?(ReplayLogger)
config.dotenv.logger = new_logger
end

# The current environment that the app is running in.
#
# When running `rake`, the Rails application is initialized in development, so we have to
Expand Down Expand Up @@ -88,11 +94,9 @@ def self.load
end

initializer "dotenv", after: :initialize_logger do |app|
# Set up a new logger once Rails has initialized the logger and replay logs
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
if logger.is_a?(ReplayLogger)
self.logger = ActiveSupport::TaggedLogging.new(::Rails.logger).tagged("dotenv")
end
end

initializer "dotenv.deprecator" do |app|
Expand Down
32 changes: 16 additions & 16 deletions spec/dotenv/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,6 @@
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 Expand Up @@ -208,7 +193,22 @@
application.initialize!

expect(Dotenv::Rails.logger).not_to be_a(Dotenv::ReplayLogger)
expect(log_output.string).to include("test")
expect(log_output.string).to include("[dotenv] test")
end

it "does not replace custom logger" do
logger = Logger.new(log_output)

Dotenv::Rails.logger = logger
application.initialize!
expect(Dotenv::Rails.logger).to be(logger)
end

# semantic-logger and older versions of rails require a block with tagged
# https://github.com/bkeepers/dotenv/issues/493
it "does not call #tagged on logger" do
expect(application.config.logger).not_to receive(:tagged)
application.initialize!
end
end
end

0 comments on commit 3e0b183

Please sign in to comment.