Class: ActiveRecord::LogSubscriber

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

Instance Method Summary collapse

Instance Method Details

#sql(event) ⇒ Object



4
5
6
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
41
# File 'lib/rails_semantic_logger/extensions/active_record/log_subscriber.rb', line 4

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] = binds = {}
    if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 0 && Rails::VERSION::TINY <= 2
      payload[:binds].each do |attr|
        attr_name, value = render_bind(attr)
        binds[attr_name] = value
      end
    elsif Rails::VERSION::MAJOR >= 5
      casted_params = type_casted_binds(payload[:binds], payload[:type_casted_binds])
      payload[:binds].zip(casted_params).map { |attr, value|
        render_bind(attr, value)
      }
    else
      payload[:binds].each do |col, v|
        attr_name, value = render_bind(col, v)
        binds[attr_name] = value
      end
    end
  end
  debug(log)
end