Sha256: 470a9b226360ff06e187fc85c303a5b85593baec5984f838572d3c623384271c

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require "active_support/subscriber"
require "active_support/log_subscriber"

module Elasticity
  GRAY = "\e[90m"

  class LogSubscriber < ::ActiveSupport::LogSubscriber
    InstrumentedClient::INDICES_METHODS.each do |method_name|
      define_method("index_#{method_name}") do |event|
        log_event(event)
      end
    end

    InstrumentedClient::INDEX_METHODS.each do |method_name|
      define_method(method_name) do |event|
        log_event(event)
      end
    end

    def multi_search(event)
      log_event(event)
    end

    private

    def log_event(event)
      bt = event.payload[:backtrace]

      if bt.present? && defined?(Rails)
        bt = Rails.backtrace_cleaner.clean(bt)
      end

      message = "#{event.transaction_id} #{event.name} #{"%.2f" % event.duration}ms #{MultiJson.dump(event.payload[:args], pretty: Elasticity.config.pretty_json)}"

      if bt = event.payload[:backtrace]
        bt = Rails.backtrace_cleaner.clean(bt) if defined?(Rails)
        lines = bt[0,4].map { |l| color(l, GRAY) }.join("\n")
        message << "\n#{lines}"
      end

      debug(message)

      exception, message = event.payload[:exception]
      if exception
        error("#{event.transaction_id} #{event.name} ERROR #{exception}: #{message}")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
es-elasticity-1.0.0 lib/elasticity/log_subscriber.rb
es-elasticity-1.0.0.jhumphreys lib/elasticity/log_subscriber.rb