Class: SequelRails::Railties::LogSubscriber

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.countObject



16
17
18
# File 'lib/sequel_rails/railties/log_subscriber.rb', line 16

def self.count
  Thread.current['sequel_sql_count'] ||= 0
end

.count=(value) ⇒ Object



12
13
14
# File 'lib/sequel_rails/railties/log_subscriber.rb', line 12

def self.count=(value)
  Thread.current['sequel_sql_count'] = value
end

.reset_countObject



26
27
28
29
30
# File 'lib/sequel_rails/railties/log_subscriber.rb', line 26

def self.reset_count
  previous = count
  self.count = 0
  previous
end

.reset_runtimeObject



20
21
22
23
24
# File 'lib/sequel_rails/railties/log_subscriber.rb', line 20

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

.runtimeObject



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

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

.runtime=(value) ⇒ Object



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

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

Instance Method Details

#loggerObject



57
58
59
# File 'lib/sequel_rails/railties/log_subscriber.rb', line 57

def logger
  ::SequelRails.configuration.logger
end

#odd?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/sequel_rails/railties/log_subscriber.rb', line 53

def odd?
  @odd_or_even = !@odd_or_even
end

#sql(event) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sequel_rails/railties/log_subscriber.rb', line 32

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

  payload = event.payload

  name = format('%s (%.1fms)', payload[:name], event.duration)
  sql  = payload[:sql].squeeze(' ')
  binds = " #{payload[:binds].inspect}" unless (payload[:binds] || []).empty?

  if odd?
    name = color(name, :cyan, :bold => true)
    sql  = color(sql, nil, :bold => true)
  else
    name = color(name, :magenta, :bold => true)
  end

  debug "  #{name}  #{sql}#{binds}"
end