Browse files

Merge pull request #58 from spastorino/master

Use rack.logger if it's avaiable
  • Loading branch information...
2 parents ded6740 + 805001b commit 06d089a28e118f091bc4a17f52af944a4626ba6c @rtomayko committed Mar 5, 2012
Showing with 30 additions and 2 deletions.
  1. +15 −2 lib/rack/cache/context.rb
  2. +15 −0 test/context_test.rb
View
17 lib/rack/cache/context.rb
@@ -79,7 +79,7 @@ def call!(env)
if verbose?
message = "cache: [%s %s] %s\n" %
[@request.request_method, @request.fullpath, trace]
- @env['rack.errors'].write(message)
+ log_info(message)
end
# tidy up response a bit
@@ -280,7 +280,20 @@ def strip_ignore_headers(response)
end
def log_error(exception)
- @env['rack.errors'].write("cache error: #{exception.message}\n#{exception.backtrace.join("\n")}\n")
+ message = "cache error: #{exception.message}\n#{exception.backtrace.join("\n")}\n"
+ log(:error, message)
+ end
+
+ def log_info(message)
+ log(:info, message)
+ end
+
+ def log(level, message)
+ if @env['rack.logger']
+ @env['rack.logger'].send(level, message)
+ else
+ @env['rack.errors'].write(message)
+ end
end
end
end
View
15 test/context_test.rb
@@ -913,4 +913,19 @@
response.body.should.equal 'Hello World'
cache.trace.should.include :pass
end
+
+ it 'logs to rack.logger if available' do
+ logger = Class.new do
+ attr_reader :logged_level
+
+ def info(message)
+ @logged_level = "info"
+ end
+ end.new
+
+ respond_with 200
+ get '/', 'rack.logger' => logger
+ response.should.be.ok
+ logger.logged_level.should.equal "info"
+ end
end

0 comments on commit 06d089a

Please sign in to comment.