Sha256: d18326d9fc886bf9f9e14cacb35bd624e3df70c51b242b19293d28e6b44f191d

Contents?: true

Size: 1.45 KB

Versions: 101

Compression:

Stored size: 1.45 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|
          [col.name, v]
        }.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

101 entries across 76 versions & 10 rubygems

Version Path
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/log_subscriber.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.12/lib/active_record/log_subscriber.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/log_subscriber.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/log_subscriber.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/log_subscriber.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/log_subscriber.rb
challah-1.0.0.beta vendor/bundle/gems/activerecord-3.2.11/lib/active_record/log_subscriber.rb
active_mailer-0.0.5 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/log_subscriber.rb
active_mailer-0.0.4 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/log_subscriber.rb
active_mailer-0.0.3 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/log_subscriber.rb
activerecord-3.2.12 lib/active_record/log_subscriber.rb
challah-0.9.1.beta.3 vendor/bundle/gems/activerecord-3.2.11/lib/active_record/log_subscriber.rb
devise_sociable-0.1.0 vendor/bundle/gems/activerecord-3.2.11/lib/active_record/log_subscriber.rb
challah-0.9.1.beta vendor/bundle/gems/activerecord-3.2.11/lib/active_record/log_subscriber.rb
activerecord-3.2.11 lib/active_record/log_subscriber.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.10/lib/active_record/log_subscriber.rb
challah-0.9.0 vendor/bundle/gems/activerecord-3.2.9/lib/active_record/log_subscriber.rb
activerecord-3.2.10 lib/active_record/log_subscriber.rb
challah-rolls-0.2.0 vendor/bundle/gems/activerecord-3.2.8/lib/active_record/log_subscriber.rb
challah-rolls-0.2.0 vendor/bundle/gems/activerecord-3.2.7/lib/active_record/log_subscriber.rb