Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tagged logger already configured on Rails #489

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/dotenv/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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