Sha256: 0e4a541361a6d1b77c8571bc0295da4a42105ed03b6c02b57cc9b0f61ca8e00d

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

module LogjamAgent
  module Rack
    class Logger < ActiveSupport::LogSubscriber
      def initialize(app)
        @app = app
        @hostname = LogjamAgent.hostname
      end

      def call(env)
        start_time = Time.now
        before_dispatch(env, start_time)
        result = @app.call(env)
      ensure
        run_time = Time.now - start_time
        after_dispatch(env, result, run_time*1000)
      end

      protected

      def before_dispatch(env, start_time)
        TimeBandits.reset

        Thread.current[:time_bandits_completed_info] = nil

        request = ActionDispatch::Request.new(env)
        path = request.filtered_path

        Rails.logger.request.fields.merge!(:started_at => start_time.iso8601, :ip => request.ip, :host => @hostname)

        info "\n\nStarted #{request.request_method} \"#{path}\" for #{request.ip} at #{start_time.to_default_s}"
      end

      def after_dispatch(env, result, run_time_ms)
        status = result ? result.first : 500
        duration, additions, view_time, action = Thread.current[:time_bandits_completed_info]

        basic_request_info = {:total_time => run_time_ms, :code => status, :action => action, :view_time => view_time || 0.0}

        message = "Completed #{status} #{::Rack::Utils::HTTP_STATUS_CODES[status]} in %.1fms" % run_time_ms
        message << " (#{additions.join(' | ')})" unless additions.blank?
        info message

        ActiveSupport::LogSubscriber.flush_all!

        Rails.logger.request.fields.merge!(basic_request_info)

        env["time_bandits.metrics"] = TimeBandits.metrics
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
logjam_agent-0.4.5 lib/logjam_agent/rack/logger.rb
logjam_agent-0.4.4 lib/logjam_agent/rack/logger.rb
logjam_agent-0.4.3 lib/logjam_agent/rack/logger.rb