Sha256: 8d9c5cce9cddf1d2441fc8fad7f959bfa87c8d03d07f65fdbc4d83686e40419c

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

require 'time'

module Rack
  module Ougai
    class LogRequests
      def initialize(app, logger = nil, local: false)
        @app = app
        @logger = logger
        @local = local
      end

      def call(env)
        start_time = Time.now
        status, headers, _body = @app.call(env)
      ensure
        logger = @logger || env[RACK_LOGGER]
        logger.info('http', create_log(start_time, env, status, headers))
      end

      private

      def create_log(start_time, env, status, _headers)
        end_time = Time.now

        ret = {
          time: @local ? start_time : start_time.utc,
          usec: end_time.usec - start_time.usec,
          remote_addr: env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"],
          method: env[REQUEST_METHOD],
          path: env[PATH_INFO],
          query: env[QUERY_STRING],
          status: status.to_i,
        }

        request_id = env['HTTP_X_REQUEST_ID']
        ret[:request_id] = request_id unless request_id.nil?

        ret
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-ougai-0.3.0 lib/rack/ougai/log_requests.rb
rack-ougai-0.2.1 lib/rack/ougai/log_requests.rb