2011-10-21 02:07:24 -04:00
|
|
|
require 'rack/commonlogger'
|
|
|
|
|
|
|
|
module Rack
|
2011-12-01 18:23:14 -05:00
|
|
|
# Patch CommonLogger to use after_reply.
|
|
|
|
#
|
|
|
|
# Simply request this file and CommonLogger will be a bit more
|
|
|
|
# efficient.
|
2011-10-21 02:07:24 -04:00
|
|
|
class CommonLogger
|
|
|
|
remove_method :call
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
began_at = Time.now
|
|
|
|
status, header, body = @app.call(env)
|
|
|
|
header = Utils::HeaderHash.new(header)
|
|
|
|
|
2013-07-18 15:55:40 -04:00
|
|
|
# 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']
|
2011-10-21 02:07:24 -04:00
|
|
|
ary << lambda { log(env, status, header, began_at) }
|
|
|
|
else
|
|
|
|
body = BodyProxy.new(body) { log(env, status, header, began_at) }
|
|
|
|
end
|
|
|
|
|
|
|
|
[status, header, body]
|
|
|
|
end
|
2013-07-18 15:55:40 -04:00
|
|
|
|
|
|
|
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
|
2011-10-21 02:07:24 -04:00
|
|
|
end
|
|
|
|
end
|