Skip to content

Commit

Permalink
Merge pull request rtomayko#58 from spastorino/master
Browse files Browse the repository at this point in the history
Use rack.logger if it's avaiable
  • Loading branch information
rtomayko committed Mar 5, 2012
2 parents ded6740 + 805001b commit 06d089a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/rack/cache/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
15 changes: 15 additions & 0 deletions test/context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.