Skip to content
This repository
Browse code

Add timezone to CommonLogger, as per spec.

Closes #550
  • Loading branch information...
commit b74afa9573454c6807d90454d9ec62948fbac5dd 1 parent 1cefdc0
James Tucker authored April 29, 2013
4  lib/rack/commonlogger.rb
@@ -18,7 +18,7 @@ module Rack
18 18
   class CommonLogger
19 19
     # Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
20 20
     #
21  
-    #   lilith.local - - [07/Aug/2006 23:58:02] "GET / HTTP/1.1" 500 -
  21
+    #   lilith.local - - [07/Aug/2006 23:58:02 -0400] "GET / HTTP/1.1" 500 -
22 22
     #
23 23
     #   %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
24 24
     FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n}
@@ -46,7 +46,7 @@ def log(env, status, header, began_at)
46 46
       logger.write FORMAT % [
47 47
         env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
48 48
         env["REMOTE_USER"] || "-",
49  
-        now.strftime("%d/%b/%Y %H:%M:%S"),
  49
+        now.strftime("%d/%b/%Y %H:%M:%S %z"),
50 50
         env["REQUEST_METHOD"],
51 51
         env["PATH_INFO"],
52 52
         env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
26  test/spec_commonlogger.rb
@@ -47,6 +47,32 @@
47 47
     res.errors.should =~ /"GET \/ " 200 - /
48 48
   end
49 49
 
  50
+  def with_mock_time(t = 0)
  51
+    mc = class <<Time; self; end
  52
+    mc.send :alias_method, :old_now, :now
  53
+    mc.send :define_method, :now do
  54
+      at(t)
  55
+    end
  56
+    yield
  57
+  ensure
  58
+    mc.send :alias_method, :now, :old_now
  59
+  end
  60
+
  61
+  should "log in common log format" do
  62
+    log = StringIO.new
  63
+    with_mock_time do
  64
+      Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/")
  65
+    end
  66
+
  67
+    md = /- - - \[([^\]]+)\] "(\w+) \/ " (\d{3}) \d+ ([\d\.]+)/.match(log.string)
  68
+    md.should.not == nil
  69
+    time, method, status, duration = *md.captures
  70
+    time.should == Time.at(0).strftime("%d/%b/%Y %H:%M:%S %z")
  71
+    method.should == "GET"
  72
+    status.should == "200"
  73
+    (0..1).should.include?(duration.to_f)
  74
+  end
  75
+
50 76
   def length
51 77
     123
52 78
   end

0 notes on commit b74afa9

Please sign in to comment.
Something went wrong with that request. Please try again.