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   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 25
25:     def <<(str)
26:       @env["rack.errors"].write(str)
27:       @env["rack.errors"].flush
28:     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 30
30:     def each
31:       length = 0
32:       @body.each { |part|
33:         length += part.size
34:         yield part
35:       }
36: 
37:       @now = Time.now
38: 
39:       # Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
40:       # lilith.local - - [07/Aug/2006 23:58:02] "GET / HTTP/1.1" 500 -
41:       #             %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
42:       @logger << %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n} %
43:         [@env["REMOTE_ADDR"] || "-",
44:          @env["REMOTE_USER"] || "-",
45:          @now.strftime("%d/%b/%Y %H:%M:%S"),
46:          @env["REQUEST_METHOD"],
47:          @env["PATH_INFO"],
48:          @env["QUERY_STRING"].empty? ? "" : "?"+@env["QUERY_STRING"],
49:          @env["HTTP_VERSION"],
50:          @status.to_s[0..3],
51:          (length.zero? ? "-" : length.to_s),
52:          @now - @time
53:         ]
54:     end

[Validate]