Class: ActiveRecord::LogSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_semantic_logger/extensions/active_record/log_subscriber.rb

Constant Summary collapse

IGNORE_PAYLOAD_NAMES =

Support Rails 3.2

['SCHEMA', 'EXPLAIN']

Instance Method Summary collapse

Instance Method Details

#sql(event) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rails_semantic_logger/extensions/active_record/log_subscriber.rb', line 7

def sql(event)
  self.class.runtime += event.duration

  return unless logger.debug?

  payload = event.payload
  name    = payload[:name]
  return if IGNORE_PAYLOAD_NAMES.include?(name)

  log_payload = {
    sql: payload[:sql],
  }
  log         = {
    message:  name,
    payload:  log_payload,
    duration: event.duration
  }

  unless (payload[:binds] || []).empty?
    log_payload[:binds] =
      if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 0 && Rails::VERSION::TINY <= 2 # 5.0.0 - 5.0.2
        bind_values_v5_0_0(payload)
      elsif Rails::VERSION::MAJOR >= 5 && Rails::VERSION::MINOR <= 1 && (Rails::VERSION::MINOR == 0 || Rails::VERSION::TINY <= 4) # 5.0.3 - 5.1.4
        bind_values_v5_0_3(payload)
      elsif Rails::VERSION::MAJOR >= 5 # >= 5.1.5
        bind_values_v5_1_5(payload)
      elsif Rails.version.to_i >= 4 # 4.x
        bind_values_v4(payload)
      else # 3.x
        bind_values_v3(payload)
      end
  end
  debug(log)
end