123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::LogSubscriber

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveSupport::LogSubscriber
Defined in: activerecord/lib/active_record/log_subscriber.rb

Constant Summary

::ActiveSupport::LogSubscriber - Inherited

BLACK, BLUE, CYAN, GREEN, LEVEL_CHECKS, MAGENTA, MODES, RED, WHITE, YELLOW

Class Attribute Summary

Class Method Summary

::ActiveSupport::LogSubscriber - Inherited

::ActiveSupport::Subscriber - Inherited

.attach_to

Attach the subscriber to a namespace.

.detach_from

Detach the subscriber from a namespace.

.method_added

Adds event subscribers for all new methods added to the class.

.new, .subscribers, .add_event_subscriber, .fetch_public_methods, .find_attached_subscriber, .invalid_event?, .pattern_subscribed?, .prepare_pattern, .remove_event_subscriber

Instance Attribute Summary

Instance Method Summary

Constructor Details

This class inherits a constructor from ActiveSupport::LogSubscriber

Class Attribute Details

.backtrace_cleaner (rw)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 7

class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new

.backtrace_cleaner?Boolean (rw)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 7

class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new

Instance Attribute Details

#backtrace_cleaner (rw)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 7

class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new

#backtrace_cleaner?Boolean (rw)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 7

class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new

Instance Method Details

#colorize_payload_name(name, payload_name) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 80

def colorize_payload_name(name, payload_name)
  if payload_name.blank? || payload_name == "SQL" # SQL vs Model Load/Exists
    color(name, MAGENTA, bold: true)
  else
    color(name, CYAN, bold: true)
  end
end

#debug(progname = nil, &block) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 113

def debug(progname = nil, &block)
  return unless super

  if ActiveRecord.verbose_query_logs
    log_query_source
  end
end

#filter(name, value) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 137

def filter(name, value)
  ActiveRecord::Base.inspection_filter.filter_param(name, value)
end

#log_query_source (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 121

def log_query_source
  source = query_source_location

  if source
    logger.debug("#{source}")
  end
end

#logger (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 109

def logger
  ActiveRecord::Base.logger
end

#query_source_location (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 129

def query_source_location
  Thread.each_caller_location do |location|
    frame = backtrace_cleaner.clean_frame(location)
    return frame if frame
  end
  nil
end

#render_bind(attr, value) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 65

def render_bind(attr, value)
  case attr
  when ActiveModel::Attribute
    if attr.type.binary? && attr.value
      value = "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>"
    end
  when Array
    attr = attr.first
  else
    attr = nil
  end

  [attr&.name, value]
end

#sql(event)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 18

def sql(event)
  payload = event.payload

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

  name = if payload[:async]
    "ASYNC #{payload[:name]} (#{payload[:lock_wait].round(1)}ms) (db time #{event.duration.round(1)}ms)"
  else
    "#{payload[:name]} (#{event.duration.round(1)}ms)"
  end
  name  = "CACHE #{name}" if payload[:cached]
  sql   = payload[:sql]
  binds = nil

  if payload[:binds]&.any?
    casted_params = type_casted_binds(payload[:type_casted_binds])

    binds = []
    payload[:binds].each_with_index do |attr, i|
      attribute_name = if attr.respond_to?(:name)
        attr.name
      elsif attr.respond_to?(:[]) && attr[i].respond_to?(:name)
        attr[i].name
      else
        nil
      end

      filtered_params = filter(attribute_name, casted_params[i])

      binds << render_bind(attr, filtered_params)
    end
    binds = binds.inspect
    binds.prepend("  ")
  end

  name = colorize_payload_name(name, payload[:name])
  sql  = color(sql, sql_color(sql), bold: true) if colorize_logging

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

#sql_color(sql) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 88

def sql_color(sql)
  case sql
  when /\A\s*rollback/mi
    RED
  when /select .*for update/mi, /\A\s*lock/mi
    WHITE
  when /\A\s*select/i
    BLUE
  when /\A\s*insert/i
    GREEN
  when /\A\s*update/i
    YELLOW
  when /\A\s*delete/i
    RED
  when /transaction\s*\Z/i
    CYAN
  else
    MAGENTA
  end
end

#strict_loading_violation(event)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 9

def strict_loading_violation(event)
  debug do
    owner = event.payload[:owner]
    reflection = event.payload[:reflection]
    color(reflection.strict_loading_violation_message(owner), RED)
  end
end

#type_casted_binds(casted_binds) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/log_subscriber.rb', line 61

def type_casted_binds(casted_binds)
  casted_binds.respond_to?(:call) ? casted_binds.call : casted_binds
end