Class: Searchkick::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/searchkick/log_subscriber.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reset_runtimeObject



12
13
14
15
16
# File 'lib/searchkick/log_subscriber.rb', line 12

def self.reset_runtime
  rt = runtime
  self.runtime = 0
  rt
end

.runtimeObject



8
9
10
# File 'lib/searchkick/log_subscriber.rb', line 8

def self.runtime
  Thread.current[:searchkick_runtime] ||= 0
end

.runtime=(value) ⇒ Object



4
5
6
# File 'lib/searchkick/log_subscriber.rb', line 4

def self.runtime=(value)
  Thread.current[:searchkick_runtime] = value
end

Instance Method Details

#multi_search(event) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/searchkick/log_subscriber.rb', line 47

def multi_search(event)
  self.class.runtime += event.duration
  return unless logger.debug?

  payload = event.payload
  name = "#{payload[:name]} (#{event.duration.round(1)}ms)"

  debug "  #{color(name, YELLOW, bold: true)}  _msearch #{payload[:body]}"
end

#request(event) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/searchkick/log_subscriber.rb', line 37

def request(event)
  self.class.runtime += event.duration
  return unless logger.debug?

  payload = event.payload
  name = "#{payload[:name]} (#{event.duration.round(1)}ms)"

  debug "  #{color(name, YELLOW, bold: true)}  #{payload.except(:name).to_json}"
end

#search(event) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/searchkick/log_subscriber.rb', line 18

def search(event)
  self.class.runtime += event.duration
  return unless logger.debug?

  payload = event.payload
  name = "#{payload[:name]} (#{event.duration.round(1)}ms)"

  index = payload[:query][:index].is_a?(Array) ? payload[:query][:index].join(",") : payload[:query][:index]
  type = payload[:query][:type]
  request_params = payload[:query].except(:index, :type, :body)

  params = []
  request_params.each do |k, v|
    params << "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
  end

  debug "  #{color(name, YELLOW, bold: true)}  #{index}#{type ? "/#{type.join(',')}" : ''}/_search#{params.any? ? '?' + params.join('&') : nil} #{payload[:query][:body].to_json}"
end