Sha256: 41cee3b6e9044668c989dc024414939e05ba6c44e39e2b53caf31b187d68d063

Contents?: true

Size: 1.51 KB

Versions: 37

Compression:

Stored size: 1.51 KB

Contents

module ActiveRecord
  class LogSubscriber < ActiveSupport::LogSubscriber
    def self.runtime=(value)
      Thread.current["active_record_sql_runtime"] = value
    end

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

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

    def initialize
      super
      @odd_or_even = false
    end

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

      payload = event.payload

      return if 'SCHEMA' == payload[:name]

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

      unless (payload[:binds] || []).empty?
        binds = "  " + payload[:binds].map { |col,v|
          if col
            [col.name, v]
          else
            [nil, v]
          end
        }.inspect
      end

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

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

    def identity(event)
      return unless logger.debug?

      name = color(event.payload[:name], odd? ? CYAN : MAGENTA, true)
      line = odd? ? color(event.payload[:line], nil, true) : event.payload[:line]

      debug "  #{name}  #{line}"
    end

    def odd?
      @odd_or_even = !@odd_or_even
    end

    def logger
      ActiveRecord::Base.logger
    end
  end
end

ActiveRecord::LogSubscriber.attach_to :active_record

Version data entries

37 entries across 32 versions & 6 rubygems

Version Path
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/activerecord-3.2.22.5/lib/active_record/log_subscriber.rb
activerecord-3.2.22.5 lib/active_record/log_subscriber.rb
activerecord-3.2.22.4 lib/active_record/log_subscriber.rb
activerecord-3.2.22.3 lib/active_record/log_subscriber.rb
activerecord-3.2.22.2 lib/active_record/log_subscriber.rb
activerecord-3.2.22.1 lib/active_record/log_subscriber.rb
activerecord-3.2.22 lib/active_record/log_subscriber.rb
activerecord-3.2.21 lib/active_record/log_subscriber.rb
apl-library-0.0.90 vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/log_subscriber.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/log_subscriber.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/log_subscriber.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/log_subscriber.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/log_subscriber.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/log_subscriber.rb
activerecord-3.2.20 lib/active_record/log_subscriber.rb
activerecord-3.2.19 lib/active_record/log_subscriber.rb
activerecord-3.2.18 lib/active_record/log_subscriber.rb
activerecord-3.2.17 lib/active_record/log_subscriber.rb
activerecord-3.2.16 lib/active_record/log_subscriber.rb
activerecord-3.2.15 lib/active_record/log_subscriber.rb