From c8bf81fe1768d2d2b1526e3ceee4c67132bd4e09 Mon Sep 17 00:00:00 2001 From: Shane Cavanaugh Date: Mon, 8 May 2017 12:11:20 -0400 Subject: [PATCH] Fallback to ::Logger if Rails::Logger not loaded (#3) Some rake tasks do not load the environment, and so ::Rails::Logger is nil. In those cases, we fallback to ::Logger.new(STDOUT) --- lib/rails-callback_log.rb | 8 ++++++-- spec/rails-callback_log_spec.rb | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/rails-callback_log.rb b/lib/rails-callback_log.rb index 7cddfb5..930c1ea 100644 --- a/lib/rails-callback_log.rb +++ b/lib/rails-callback_log.rb @@ -9,6 +9,10 @@ module RailsCallbackLog FILTER = ENV["RAILS_CALLBACK_LOG_FILTER"].present?.freeze class << self + def logger + ::Rails.logger || ::Logger.new(STDOUT) + end + def matches_filter?(str) source_location_filters.any? { |f| str.start_with?(f) } end @@ -28,7 +32,7 @@ def make_lambda lambda { |*args, &block| if !::RailsCallbackLog::FILTER || caller.any? { |line| ::RailsCallbackLog.matches_filter?(line) } - ::Rails.logger.debug(format("Callback: %s", @method_name)) + ::RailsCallbackLog.logger.debug(format("Callback: %s", @method_name)) end original_lambda.call(*args, &block) } @@ -43,7 +47,7 @@ def make_lambda(filter) lambda { |*args, &block| if !::RailsCallbackLog::FILTER || caller.any? { |line| ::RailsCallbackLog.matches_filter?(line) } - ::Rails.logger.debug(format("Callback: %s", filter)) + ::RailsCallbackLog.logger.debug(format("Callback: %s", filter)) end original_lambda.call(*args, &block) } diff --git a/spec/rails-callback_log_spec.rb b/spec/rails-callback_log_spec.rb index 560fbf2..e64f1b9 100644 --- a/spec/rails-callback_log_spec.rb +++ b/spec/rails-callback_log_spec.rb @@ -16,7 +16,7 @@ module Callbacks # Mock the rails logger. We're going to expect it to receive a `debug` message. logger = double allow(logger).to receive(:debug) - allow(::Rails).to receive(:logger).and_return(logger) + allow(::RailsCallbackLog).to receive(:logger).and_return(logger) target = double # Symbol-based callbacks have a `target` and a `value`. The definition of