summary refs log tree commit
diff options
context:
space:
mode:
authorJames Tucker <jftucker@gmail.com>2013-04-29 14:24:37 -0700
committerJames Tucker <jftucker@gmail.com>2013-04-29 14:24:37 -0700
commitb74afa9573454c6807d90454d9ec62948fbac5dd (patch)
treed52929a662e6678dc35e4fb55a313d141a68f2e0
parent1cefdc0503dd92bf7c260bc85e4a67ded9ceeda6 (diff)
downloadrack-b74afa9573454c6807d90454d9ec62948fbac5dd.tar.gz
Add timezone to CommonLogger, as per spec.
Closes #550
-rw-r--r--lib/rack/commonlogger.rb4
-rw-r--r--test/spec_commonlogger.rb26
2 files changed, 28 insertions, 2 deletions
diff --git a/lib/rack/commonlogger.rb b/lib/rack/commonlogger.rb
index 7028615f..b3968ac7 100644
--- a/lib/rack/commonlogger.rb
+++ b/lib/rack/commonlogger.rb
@@ -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 @@ module Rack
       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"],
diff --git a/test/spec_commonlogger.rb b/test/spec_commonlogger.rb
index d88e19c3..9c96638c 100644
--- a/test/spec_commonlogger.rb
+++ b/test/spec_commonlogger.rb
@@ -47,6 +47,32 @@ describe Rack::CommonLogger do
     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