Sha256: 128215d69a32529a9c7f17dfec71fdee407bec9447ef8febe4dc21db41104440

Contents?: true

Size: 1.27 KB

Versions: 61

Compression:

Stored size: 1.27 KB

Contents

require 'rack/commonlogger'

module Rack
  # Patch CommonLogger to use after_reply.
  #
  # Simply request this file and CommonLogger will be a bit more 
  # efficient.
  class CommonLogger
    remove_method :call

    def call(env)
      began_at = Time.now
      status, header, body = @app.call(env)
      header = Utils::HeaderHash.new(header)

      # If we've been hijacked, then output a special line
      if env['rack.hijack_io']
        log_hijacking(env, 'HIJACK', header, began_at)
      elsif ary = env['rack.after_reply']
        ary << lambda { log(env, status, header, began_at) }
      else
        body = BodyProxy.new(body) { log(env, status, header, began_at) }
      end

      [status, header, body]
    end

    HIJACK_FORMAT = %{%s - %s [%s] "%s %s%s %s" HIJACKED -1 %0.4f\n}

    def log_hijacking(env, status, header, began_at)
      now = Time.now

      logger = @logger || env['rack.errors']
      logger.write HIJACK_FORMAT % [
        env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
        env["REMOTE_USER"] || "-",
        now.strftime("%d/%b/%Y %H:%M:%S"),
        env["REQUEST_METHOD"],
        env["PATH_INFO"],
        env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
        env["HTTP_VERSION"],
        now - began_at ]
    end
  end
end

Version data entries

61 entries across 61 versions & 4 rubygems

Version Path
puma-2.9.0-java lib/puma/rack_patch.rb
puma-2.9.0 lib/puma/rack_patch.rb
rsense-server-0.5.17 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.16 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.15 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.14 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.13 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.12 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.11 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.10 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.9 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.8 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.7 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.6 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.5 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.4 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.2 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
rsense-server-0.5.0 vendor/gems/puma-2.8.2-java/lib/puma/rack_patch.rb
puma-2.8.2-java lib/puma/rack_patch.rb
puma-2.8.2 lib/puma/rack_patch.rb