5e68ac626dc44729c963d9eef32f4b287964a109
[feedcatcher.git] / vendor / rails / actionpack / lib / action_controller / vendor / rack-1.0 / rack / commonlogger.rb
1 module Rack
2 # Rack::CommonLogger forwards every request to an +app+ given, and
3 # logs a line in the Apache common log format to the +logger+, or
4 # rack.errors by default.
5
6 class CommonLogger
7 def initialize(app, logger=nil)
8 @app = app
9 @logger = logger
10 end
11
12 def call(env)
13 dup._call(env)
14 end
15
16 def _call(env)
17 @env = env
18 @logger ||= self
19 @time = Time.now
20 @status, @header, @body = @app.call(env)
21 [@status, @header, self]
22 end
23
24 def close
25 @body.close if @body.respond_to? :close
26 end
27
28 # By default, log to rack.errors.
29 def <<(str)
30 @env["rack.errors"].write(str)
31 @env["rack.errors"].flush
32 end
33
34 def each
35 length = 0
36 @body.each { |part|
37 length += part.size
38 yield part
39 }
40
41 @now = Time.now
42
43 # Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
44 # lilith.local - - [07/Aug/2006 23:58:02] "GET / HTTP/1.1" 500 -
45 # %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
46 @logger << %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n} %
47 [
48 @env['HTTP_X_FORWARDED_FOR'] || @env["REMOTE_ADDR"] || "-",
49 @env["REMOTE_USER"] || "-",
50 @now.strftime("%d/%b/%Y %H:%M:%S"),
51 @env["REQUEST_METHOD"],
52 @env["PATH_INFO"],
53 @env["QUERY_STRING"].empty? ? "" : "?"+@env["QUERY_STRING"],
54 @env["HTTP_VERSION"],
55 @status.to_s[0..3],
56 (length.zero? ? "-" : length.to_s),
57 @now - @time
58 ]
59 end
60 end
61 end