diff --git a/README.md b/README.md index 1272c77..dee6856 100644 --- a/README.md +++ b/README.md @@ -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`. @@ -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 diff --git a/lib/dotenv/rails.rb b/lib/dotenv/rails.rb index ab05c3c..cace914 100644 --- a/lib/dotenv/rails.rb +++ b/lib/dotenv/rails.rb @@ -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() @@ -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 @@ -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| diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index bdcd1f5..f50088a 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -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) @@ -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