Skip to content

Commit

Permalink
Fallback to ::Logger if Rails::Logger not loaded (#3)
Browse files Browse the repository at this point in the history
Some rake tasks do not load the environment, and so ::Rails::Logger is nil. In those cases, we fallback to ::Logger.new(STDOUT)
  • Loading branch information
shanecav84 authored and jaredbeck committed May 8, 2017
1 parent 01e5974 commit c8bf81f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/rails-callback_log.rb
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion spec/rails-callback_log_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit c8bf81f

Please sign in to comment.