Class: Rack::CommonLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/rack_patch.rb

Overview

Patch CommonLogger to use after_reply.

Simply request this file and CommonLogger will be a bit more efficient.

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/puma/rack_patch.rb', line 11

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

#log_hijacking(env, status, header, began_at) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/puma/rack_patch.rb', line 30

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