Sha256: 414dafbe75c23697bae00088fc013165535ac29833a9554f09cc14562138f69c

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

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

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

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

      def self.logger=(logger)
        @logger = logger
      end

      def self.logger
        @logger if defined?(@logger)
      end

      def logger
        self.class.logger || ::Rails.logger
      end

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

        name = '%s (%.1fms)' % ["SOLR Request", event.duration]

        # produces: path=select parameters={fq: ["type:Tag"], q: "rossi", fl: "* score", qf: "tag_name_text", defType: "edismax", start: 0, rows: 20}
        path = color(event.payload[:path], nil, bold: true)
        parameters = event.payload[:parameters].map { |k, v|
          v = "\"#{v}\"" if v.is_a? String
          v = v.to_s.gsub(/\\/,'') # unescape
          "#{k}: #{color(v, nil, bold: true)}"
        }.join(', ')
        request = "path=#{path} parameters={#{parameters}}"

        debug "  #{color(name, GREEN, bold: true)}  [ #{request} ]"
      end
    end
  end
end

Sunspot::Rails::LogSubscriber.attach_to :rsolr

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sunspot_rails-2.7.1 lib/sunspot/rails/log_subscriber.rb
sunspot_rails-2.7.0 lib/sunspot/rails/log_subscriber.rb