Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Possible fix for #412

  • Loading branch information...
commit e3772a5870c32b5f9951a8356298200ce868bf23 1 parent 734a00c
@urielka urielka authored
Showing with 15 additions and 2 deletions.
  1. +5 −2 lib/rack/commonlogger.rb
  2. +10 −0 test/spec_commonlogger.rb
View
7 lib/rack/commonlogger.rb
@@ -10,7 +10,7 @@ module Rack
# an instance of Rack::NullLogger.
#
# +logger+ can be any class, including the standard library Logger, and is
- # expected to have a +write+ method, which accepts the CommonLogger::FORMAT.
+ # expected to have either +write+ or +<<+ method, which accepts the CommonLogger::FORMAT.
# According to the SPEC, the error stream must also respond to +puts+
# (which takes a single argument that responds to +to_s+), and +flush+
# (which is called without arguments in order to make the error appear for
@@ -43,7 +43,10 @@ def log(env, status, header, began_at)
length = extract_content_length(header)
logger = @logger || env['rack.errors']
- logger.write FORMAT % [
+ #Standard library logger doesn't support write but it supports << which actually
+ #calls to write on the log device without formatting
+ write = logger.respond_to?(:write) ? logger.method(:write) : logger.method(:<<)
+ write.call FORMAT % [
env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
env["REMOTE_USER"] || "-",
now.strftime("%d/%b/%Y:%H:%M:%S %z"),
View
10 test/spec_commonlogger.rb
@@ -2,6 +2,8 @@
require 'rack/lint'
require 'rack/mock'
+require 'logger'
+
describe Rack::CommonLogger do
obj = 'foobar'
length = obj.size
@@ -33,6 +35,14 @@
log.string.should =~ /"GET \/ " 200 #{length} /
end
+ should "work with standartd library logger" do
+ logdev = StringIO.new
+ log = Logger.new(logdev)
+ Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/")
+
+ logdev.string.should =~ /"GET \/ " 200 #{length} /
+ end
+
should "log - content length if header is missing" do
res = Rack::MockRequest.new(Rack::CommonLogger.new(app_without_length)).get("/")
Please sign in to comment.
Something went wrong with that request. Please try again.