From a2e4d911473e06b02143da0df11a26698c8d4cd8 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 26 Feb 2024 12:41:06 -0500 Subject: [PATCH] Refactor ReplayLogger to extend Logger --- lib/dotenv/replay_logger.rb | 16 ++++++++-------- spec/dotenv/rails_spec.rb | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/dotenv/replay_logger.rb b/lib/dotenv/replay_logger.rb index b91e7174..dc44bee7 100644 --- a/lib/dotenv/replay_logger.rb +++ b/lib/dotenv/replay_logger.rb @@ -1,20 +1,20 @@ module Dotenv # A logger that can be used before the apps real logger is initialized. - class ReplayLogger + class ReplayLogger < Logger def initialize + super(nil) # Doesn't matter what this is, it won't be used. @logs = [] end - def method_missing(name, *args, &block) - @logs.push([name, args, block]) - end - - def respond_to_missing?(name, include_private = false) - (include_private ? Logger.instance_methods : Logger.public_instance_methods).include?(name) || super + # Override the add method to store logs so we can replay them to a real logger later. + def add(*args, &block) + @logs.push([args, block]) end + # Replay the store logs to a real logger. def replay(logger) - @logs.each { |name, args, block| logger.send(name, *args, &block) } + @logs.each { |args, block| logger.add(*args, &block) } + @logs.clear end end end diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index 3259be3f..693b7199 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -3,11 +3,13 @@ require "dotenv/rails" describe Dotenv::Rails do + let(:log_io) { StringIO.new } let(:application) do + log_io = self.log_io Class.new(Rails::Application) do config.load_defaults Rails::VERSION::STRING.to_f config.eager_load = false - config.logger = ActiveSupport::Logger.new(StringIO.new) + config.logger = ActiveSupport::Logger.new(log_io) config.root = fixture_path # Remove method fails since app is reloaded for each test @@ -197,4 +199,18 @@ application.initialize! end end + + describe "logger" do + it "defaults to ReplayLogger" do + expect(Dotenv::Rails.logger).to be_a(Dotenv::ReplayLogger) + application.initialize! + expect(Dotenv::Rails.logger).to be_a(ActiveSupport::BroadcastLogger) + end + + it "replays to Rails.logger" do + Dotenv::Rails.logger.debug("test") + application.initialize! + expect(log_io.string).to include("test") + end + end end