Class: ThinkingSphinx::ActiveRecord::LogSubscriber

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogSubscriber

Returns a new instance of LogSubscriber.



19
20
21
22
# File 'lib/thinking_sphinx/active_record/log_subscriber.rb', line 19

def initialize
  super
  @odd_or_even = false
end

Class Method Details

.reset_runtimeObject



14
15
16
17
# File 'lib/thinking_sphinx/active_record/log_subscriber.rb', line 14

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

.runtimeObject



10
11
12
# File 'lib/thinking_sphinx/active_record/log_subscriber.rb', line 10

def self.runtime
  Thread.current['thinking_sphinx_query_runtime'] ||= 0
end

.runtime=(value) ⇒ Object



6
7
8
# File 'lib/thinking_sphinx/active_record/log_subscriber.rb', line 6

def self.runtime=(value)
  Thread.current['thinking_sphinx_query_runtime'] = value
end

Instance Method Details

#loggerObject



49
50
51
52
# File 'lib/thinking_sphinx/active_record/log_subscriber.rb', line 49

def logger
  return @logger if defined? @logger
  self.logger = ::ActiveRecord::Base.logger
end

#logger=(logger) ⇒ Object



54
55
56
# File 'lib/thinking_sphinx/active_record/log_subscriber.rb', line 54

def logger=(logger)
  @logger = logger
end

#message(event) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/thinking_sphinx/active_record/log_subscriber.rb', line 35

def message(event)
  return unless logger.debug?

  identifier = color 'Sphinx', GREEN, true
  message = event.payload[:message]
  message = color message, nil, true if odd?

  debug "  #{identifier}  #{message}"
end

#odd?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/thinking_sphinx/active_record/log_subscriber.rb', line 45

def odd?
  @odd_or_even = !@odd_or_even
end

#query(event) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/thinking_sphinx/active_record/log_subscriber.rb', line 24

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

  identifier = color('Sphinx Query (%.1fms)' % event.duration, GREEN, true)
  query = event.payload[:query]
  query = color query, nil, true if odd?

  debug "  #{identifier}  #{query}"
end