Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add timezone to CommonLogger, as per spec.

Closes #550
  • Loading branch information...
commit b74afa9573454c6807d90454d9ec62948fbac5dd 1 parent 1cefdc0
James Tucker raggi authored
Showing with 28 additions and 2 deletions.
  1. +2 −2 lib/rack/commonlogger.rb
  2. +26 −0 test/spec_commonlogger.rb
4 lib/rack/commonlogger.rb
View
@@ -18,7 +18,7 @@ module Rack
class CommonLogger
# Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
#
- # lilith.local - - [07/Aug/2006 23:58:02] "GET / HTTP/1.1" 500 -
+ # lilith.local - - [07/Aug/2006 23:58:02 -0400] "GET / HTTP/1.1" 500 -
#
# %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n}
@@ -46,7 +46,7 @@ def log(env, status, header, began_at)
logger.write FORMAT % [
env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
env["REMOTE_USER"] || "-",
- now.strftime("%d/%b/%Y %H:%M:%S"),
+ now.strftime("%d/%b/%Y %H:%M:%S %z"),
env["REQUEST_METHOD"],
env["PATH_INFO"],
env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
26 test/spec_commonlogger.rb
View
@@ -47,6 +47,32 @@
res.errors.should =~ /"GET \/ " 200 - /
end
+ def with_mock_time(t = 0)
+ mc = class <<Time; self; end
+ mc.send :alias_method, :old_now, :now
+ mc.send :define_method, :now do
+ at(t)
+ end
+ yield
+ ensure
+ mc.send :alias_method, :now, :old_now
+ end
+
+ should "log in common log format" do
+ log = StringIO.new
+ with_mock_time do
+ Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/")
+ end
+
+ md = /- - - \[([^\]]+)\] "(\w+) \/ " (\d{3}) \d+ ([\d\.]+)/.match(log.string)
+ md.should.not == nil
+ time, method, status, duration = *md.captures
+ time.should == Time.at(0).strftime("%d/%b/%Y %H:%M:%S %z")
+ method.should == "GET"
+ status.should == "200"
+ (0..1).should.include?(duration.to_f)
+ end
+
def length
123
end
Please sign in to comment.
Something went wrong with that request. Please try again.