Class: ActiveRecord::Cause::LogSubscriber

Inherits:
LogSubscriber
  • Object
show all
Defined in:
lib/activerecord/cause.rb

Direct Known Subclasses

LogSubscriberAR4, LogSubscriberAR502

Constant Summary collapse

IGNORE_PAYLOAD_NAMES =
["SCHEMA", "EXPLAIN"]

Instance Method Summary collapse

Instance Method Details

#sql(event) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/activerecord/cause.rb', line 26

def sql(event)
  return unless logger.debug?

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

  locations = get_locations
  return if locations.empty?

  if ActiveRecord::Cause.log_mode != :all
    locations = locations.take(1)
  end

  locations.each do |loc|
    @is_odd = nil

    unless (payload[:binds] || []).empty?
      binds = get_binds(payload)
    end

    name = name_with_color(payload[:name])
    sql = sql_with_color(payload[:sql])
    cause = generate_cause(loc.to_s, true)

    output =
      if ActiveRecord::Cause.log_with_sql
        "  #{name}  #{sql}#{binds} caused by #{cause}"
      else
        "  #{name}  caused by #{cause}"
      end

    debug(output)
  end
end