Sha256: 6181709ec3406aaf13bcde6a500fe437bb3edfc8aaf163a84dcce21928215355

Contents?: true

Size: 1.44 KB

Versions: 5

Compression:

Stored size: 1.44 KB

Contents

module RailsLogstasher

  module Rack

    class Logger

      def initialize(app, tags = nil)
        @app, @tags = app, tags.presence
      end

      def call(env)

        t1 = Time.now
        request = ActionDispatch::Request.new(env)

        event = RailsLogstasher::Event.new(Rails.logger, true)
        event.message = "#{request.request_method} #{request.filtered_path} for #{request.ip}"
        event.fields['client_ip'] = request.ip
        event.fields['method'] = request.request_method
        event.fields['path'] = request.filtered_path
        #TODO Should really move this into the base logger
        event.source = "http://#{Socket.gethostname}#{request.filtered_path}"

        event.add_tags_to_logger(request, @tags) if @tags

        RailsLogstasher.log_entries[Thread.current] = event

        status, headers, response = @app.call(env)
        [status, headers, response]

      ensure
        if event
          event.fields['total_duration'] = Time.now - t1
          event.fields['status'] = status

          ['rendering','sql'].each do |type|
            if event.fields[type] && !event.fields[type].empty?
              duration = event.fields[type].inject(0) {|result, local_event| result += local_event[:duration].to_f }
              event.fields["#{type}_duration"] = duration
            end
          end

          event.write(true)
        end

        RailsLogstasher.log_entries[Thread.current] = nil
      end

    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails-logstasher-0.1.4 lib/rails-logstasher/rack/logger.rb
rails-logstasher-0.1.3 lib/rails-logstasher/rack/logger.rb
rails-logstasher-0.1.2 lib/rails-logstasher/rack/logger.rb
rails-logstasher-0.1.1 lib/rails-logstasher/rack/logger.rb
rails-logstasher-0.1.0 lib/rails_logstasher/rack/logger.rb