Skip to content

Commit

Permalink
Merge pull request #494 from bkeepers/semantic-logger-fix
Browse files Browse the repository at this point in the history
Fix regression with semantic-logger, allow setting custom logger
  • Loading branch information
bkeepers authored Feb 27, 2024
2 parents 87cd07e + 588f7a4 commit 90ffc9d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require 'dotenv'
Dotenv.load('file1.env', 'file2.env')
```

## Autorestore in tests
### Autorestore in tests

Since 3.0, dotenv in a Rails app will automatically restore `ENV` after each test. This means you can modify `ENV` in your tests without fear of leaking state to other tests. It works with both `ActiveSupport::TestCase` and `Rspec`.

Expand Down Expand Up @@ -181,6 +181,8 @@ Available options:

* `Dotenv::Rails.files` - list of files to be loaded, in order of precedence.
* `Dotenv::Rails.overwrite` - Overwrite exiting `ENV` variables with contents of `.env*` files
* `Dotenv::Rails.logger` - The logger to use for dotenv's logging. Defaults to `Rails.logger`
* `Dotenv::Rails.autorestore` - Enable or disable [autorestore](#autorestore-in-tests)

### Multi-line values

Expand Down
16 changes: 10 additions & 6 deletions lib/dotenv/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
module Dotenv
# Rails integration for using Dotenv to load ENV variables from a file
class Rails < ::Rails::Railtie
delegate :files, :files=, :overwrite, :overwrite=, :autorestore, :autorestore=, :logger, :logger=, to: "config.dotenv"
delegate :files, :files=, :overwrite, :overwrite=, :autorestore, :autorestore=, :logger, to: "config.dotenv"

def initialize
super()
Expand Down 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
25 changes: 9 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,15 @@
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
end
end

0 comments on commit 90ffc9d

Please sign in to comment.