Sha256: 533903fab9226f4e326b918b016c268ba915075cc2680d6760ddd0dc71b8b6cf

Contents?: true

Size: 1.25 KB

Versions: 7

Compression:

Stored size: 1.25 KB

Contents

require 'active_support/notifications'
require 'active_record/log_subscriber'
require 'logstasher/custom_fields'

module LogStasher
  module ActiveRecord
    class LogSubscriber < ::ActiveRecord::LogSubscriber
      include CustomFields::LogSubscriber

      def identity(event)
        lsevent = logstash_event(event)
        if logger && lsevent
          logger << lsevent.to_json + "\n"
        end
      end
      alias :sql :identity

      def logger
        LogStasher.logger
      end

      private

      def logstash_event(event)
        data = event.payload

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

        data.merge! runtimes(event)
        data.merge! extract_sql(data)
        data.merge! request_context
        data.merge! LogStasher.store
        data.merge! extract_custom_fields(data)

        tags = [ 'request' ]
        tags.push('exception') if data[:exception]
        LogStasher.build_logstash_event(data, tags)
      end

      def request_context
        LogStasher.request_context
      end

      def runtimes(event)
        if event.duration
          { duration: event.duration.to_f.round(2) }
        else
          {  }
        end
      end

      def extract_sql(data)
        { sql: data[:sql].squeeze(' ') }
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
logstasher-1.4.0 lib/logstasher/active_record/log_subscriber.rb
logstasher-1.3.0 lib/logstasher/active_record/log_subscriber.rb
logstasher-1.2.2 lib/logstasher/active_record/log_subscriber.rb
logstasher-1.2.1 lib/logstasher/active_record/log_subscriber.rb
logstasher-1.2.0 lib/logstasher/active_record/log_subscriber.rb
logstasher-1.1.1 lib/logstasher/active_record/log_subscriber.rb
logstasher-1.1.0 lib/logstasher/active_record/log_subscriber.rb