Sha256: d8ef5c2bd8af0dd4b913baa9ac09ad314b8156e1e17bfd20ce274fd3ad3a0a97

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

module SequelRails
  module Railties
    class LogSubscriber < ActiveSupport::LogSubscriber
      def self.runtime=(value)
        Thread.current['sequel_sql_runtime'] = value
      end

      def self.runtime
        Thread.current['sequel_sql_runtime'] ||= 0
      end

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

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

        payload = event.payload

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

        unless (payload[:binds] || []).empty?
          binds = '  ' + payload[:binds].map do |col, v|
            [col.name, 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 odd?
        @odd_or_even = !@odd_or_even
      end

      def logger
        ::SequelRails.configuration.logger
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sequel-rails-0.9.2 lib/sequel_rails/railties/log_subscriber.rb
sequel-rails-0.9.1 lib/sequel_rails/railties/log_subscriber.rb
sequel-rails-0.9.0 lib/sequel_rails/railties/log_subscriber.rb
sequel-rails-0.8.0 lib/sequel_rails/railties/log_subscriber.rb