Skip to content

Commit

Permalink
add log_initial_strategy setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Borzik committed Jul 22, 2017
1 parent a3ea198 commit e4a2895
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
8 changes: 7 additions & 1 deletion lib/chewy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ class Config
:default_field_type,

# Chewy search request DSL base class, used by every index.
:search_class
:search_class,

# Log the first strategy added to stack. May be useful to set to false
# when you don't need messages about it being added to (removed from) stack
# on every request. Will preserve logging further strategy stack updates.
:log_initial_strategy

def self.delegated
public_instance_methods - superclass.public_instance_methods - Singleton.public_instance_methods
Expand All @@ -82,6 +87,7 @@ def initialize
@default_root_options = {}
@default_field_type = 'string'.freeze
self.search_class = Chewy::Search::Request
@log_initial_strategy = true
end

def transport_logger=(logger)
Expand Down
9 changes: 6 additions & 3 deletions lib/chewy/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ def current

def push(name)
result = @stack.push resolve(name).new
debug "[#{@stack.size}] <- #{current.name}"
debug "[#{@stack.size}] <- #{current.name}" if Chewy.log_initial_strategy || @stack.length > 2
result
end

def pop
raise "Can't pop root strategy" if @stack.one?
debug "[#{@stack.size}] -> #{current.name}"
result = @stack.pop.tap(&:leave)
debug "[#{@stack.size+1}] -> #{result.name}" if Chewy.log_initial_strategy || @stack.size > 1
result
end

Expand All @@ -77,7 +77,10 @@ def wrap(name)
def debug(string)
return unless Chewy.logger && Chewy.logger.debug?
line = caller.detect { |l| l !~ %r{lib/chewy/strategy.rb:|lib/chewy.rb:} }
Chewy.logger.debug(["DEBUG: Chewy strategies stack: #{string}", line.sub(/:in\s.+$/, '')].join(' @ '))
Chewy.logger.debug([
"DEBUG: Chewy strategies stack: #{string}, now #{current.name}",
line.sub(/:in\s.+$/, '')
].join(' @ '))
end

def resolve(name)
Expand Down
1 change: 1 addition & 0 deletions spec/chewy/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
its(:reset_no_replicas) { should == false }
its(:disable_refresh_async) { should == false }
its(:search_class) { should be < Chewy::Search::Request }
its(:log_initial_strategy) { should == true }

describe '#transport_logger=' do
let(:logger) { Logger.new('/dev/null') }
Expand Down

0 comments on commit e4a2895

Please sign in to comment.