Sha256: 12fce04ee7bf19e169d142d71fbf4d9e87ae2ece544cf584299fd0ca41b8b52a

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

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

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

      def render_template(event)
        logstash_event(event)
      end
      alias :render_partial :render_template
      alias :render_collection :render_template

      def logger
        LogStasher.logger
      end

      private

      def logstash_event(event)
        data = event.payload

        data.merge! event_data(event)
        data.merge! runtimes(event)
        data.merge! extract_data(data)
        data.merge! request_context
#        data.merge! store
        data.merge! extract_custom_fields(data)

        tags = []
        tags.push('exception') if data[:exception]
        logger << LogStasher.build_logstash_event(data, tags).to_json + "\n"
      end

      def extract_data(data)
        {  identifier: from_rails_root(data[:identifier]) }
      end

      def request_context
        LogStasher.request_context
      end

      def store
        LogStasher.store
      end

      def event_data(event)
        {
          name: event.name,
          transaction_id: event.transaction_id,
        }
      end

      def runtimes(event)
        {
          duration: event.duration,
        }.inject({}) do |runtimes, (name, runtime)|
          runtimes[name] = runtime.to_f.round(2) if runtime
          runtimes
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
logstasher-1.0.1 lib/logstasher/action_view/log_subscriber.rb
logstasher-1.0.0 lib/logstasher/action_view/log_subscriber.rb