Class Rack::CommonLogger
In: lib/rack/commonlogger.rb
Parent: Object

Rack::CommonLogger forwards every request to an app given, and logs a line in the Apache common log format to the logger, or rack.errors by default.

Methods

<<   _call   call   close   each   new  

Public Class methods

[Source]

    # File lib/rack/commonlogger.rb, line 7
 7:     def initialize(app, logger=nil)
 8:       @app = app
 9:       @logger = logger
10:     end

Public Instance methods

By default, log to rack.errors.

[Source]

    # File lib/rack/commonlogger.rb, line 29
29:     def <<(str)
30:       @env["rack.errors"].write(str)
31:       @env["rack.errors"].flush
32:     end

[Source]

    # File lib/rack/commonlogger.rb, line 16
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

[Source]

    # File lib/rack/commonlogger.rb, line 12
12:     def call(env)
13:       dup._call(env)
14:     end

[Source]

    # File lib/rack/commonlogger.rb, line 24
24:     def close
25:       @body.close if @body.respond_to? :close
26:     end

[Source]

    # File lib/rack/commonlogger.rb, line 34
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

[Validate]