Class: DataMapper::Railties::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/dm-rails/railties/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/dm-rails/railties/log_subscriber.rb', line 19

def initialize
  super
  @odd_or_even = false
end

Class Method Details

.reset_runtimeObject



14
15
16
17
# File 'lib/dm-rails/railties/log_subscriber.rb', line 14

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

.runtimeObject



10
11
12
# File 'lib/dm-rails/railties/log_subscriber.rb', line 10

def self.runtime
  Thread.current["data_mapper_sql_runtime"] ||= 0
end

.runtime=(value) ⇒ Object



6
7
8
# File 'lib/dm-rails/railties/log_subscriber.rb', line 6

def self.runtime=(value)
  Thread.current["data_mapper_sql_runtime"] = value
end

Instance Method Details

#loggerObject



45
46
47
# File 'lib/dm-rails/railties/log_subscriber.rb', line 45

def logger
  ::DataMapper.logger
end

#odd?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/dm-rails/railties/log_subscriber.rb', line 41

def odd?
  @odd_or_even = !@odd_or_even
end

#sql(event) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dm-rails/railties/log_subscriber.rb', line 24

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

  name = '%s (%.3fms)' % [event.payload[:name], event.duration]
  sql  = event.payload[:sql].squeeze(' ')

  if odd?
    name = color(name, CYAN, true)
    sql  = color(sql, nil, true)
  else
    name = color(name, MAGENTA, true)
  end

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