diff --git a/lib/faraday/response/logger.rb b/lib/faraday/response/logger.rb index 51b033e7..c7fc9d6c 100644 --- a/lib/faraday/response/logger.rb +++ b/lib/faraday/response/logger.rb @@ -4,12 +4,15 @@ module Faraday class Response::Logger < Response::Middleware extend Forwardable - def initialize(app, logger = nil) + DEFAULT_OPTIONS = { :bodies => false } + + def initialize(app, logger = nil, options = {}) super(app) @logger = logger || begin require 'logger' ::Logger.new(STDOUT) end + @options = DEFAULT_OPTIONS.merge(options) end def_delegators :@logger, :debug, :info, :warn, :error, :fatal @@ -17,13 +20,14 @@ module Faraday def call(env) info "#{env.method} #{env.url.to_s}" debug('request') { dump_headers env.request_headers } - debug('request') { env[:body] } if env[:body] + debug('request') { dump_body(env[:body]) } if env[:body] && log_body?(:request) super end def on_complete(env) info('Status') { env.status.to_s } debug('response') { dump_headers env.response_headers } + debug('response') { dump_body env[:body] } if env[:body] && log_body?(:response) end private @@ -31,5 +35,21 @@ module Faraday def dump_headers(headers) headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n") end + + def dump_body(body) + if body.is_a? String + body + else + require 'pp' + body.pretty_inspect + end + end + + def log_body?(type) + case @options[:bodies] + when Hash then @options[:bodies][type] + else @options[:bodies] + end + end end end diff --git a/test/adapters/logger_test.rb b/test/adapters/logger_test.rb index 05a6c380..0a34eb96 100644 --- a/test/adapters/logger_test.rb +++ b/test/adapters/logger_test.rb @@ -4,18 +4,28 @@ require 'logger' module Adapters class LoggerTest < Faraday::TestCase + def conn(logger, logger_options={}) + rubbles = { :husband => 'Barney', :wife => 'Betty'} + + Faraday.new do |b| + b.response :logger, logger, logger_options + b.adapter :test do |stubs| + stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] } + stubs.post('/ohai') { [200, {'Content-Type' => 'text/html'}, 'fred'] } + stubs.get('/ohno') { [200, {'Content-Type' => 'text/html'}, 'wilma'] } + stubs.post('/ohyes') { [200, {'Content-Type' => 'text/html'}, 'pebbles'] } + stubs.get('/rubbles') { [200, {'Content-Type' => 'application/json'}, rubbles] } + end + end + end + def setup @io = StringIO.new @logger = Logger.new(@io) @logger.level = Logger::DEBUG - @conn = Faraday.new do |b| - b.response :logger, @logger - b.adapter :test do |stubs| - stubs.post('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] } - end - end - @resp = @conn.post '/hello', 'name=Unagi', :accept => 'text/html' + @conn = conn(@logger) + @resp = @conn.get '/hello', nil, :accept => 'text/html' end def test_still_returns_output @@ -23,19 +33,50 @@ module Adapters end def test_logs_method_and_url - assert_match "post http:/hello", @io.string + assert_match "get http:/hello", @io.string end def test_logs_request_headers assert_match %(Accept: "text/html), @io.string end - def test_logs_request_body - assert_match %(name=Unagi), @io.string - end - def test_logs_response_headers assert_match %(Content-Type: "text/html), @io.string end + + def test_does_not_log_request_body_by_default + @conn.post '/ohai', 'name=Unagi', :accept => 'text/html' + refute_match %(name=Unagi), @io.string + end + + def test_does_not_log_response_body_by_default + @conn.post '/ohai', 'name=Toro', :accept => 'text/html' + refute_match %(fred), @io.string + end + + def test_log_request_body + app = conn(@logger, :bodies => { :request => true }) + app.post '/ohyes', 'name=Tamago', :accept => 'text/html' + assert_match %(name=Tamago), @io.string + end + + def test_log_response_body + app = conn(@logger, :bodies => { :response => true }) + app.get '/ohno', :accept => 'text/html' + assert_match %(wilma), @io.string + end + + def test_log_request_and_response_body + app = conn(@logger, :bodies => true) + app.post '/ohyes', 'name=Ebi', :accept => 'text/html' + assert_match %(name=Ebi), @io.string + assert_match %(pebbles), @io.string + end + + def test_log_response_body_object + app = conn(@logger, :bodies => true) + app.get '/rubbles', nil, :accept => 'text/html' + assert_match %(:husband=>\"Barney\", :wife=>\"Betty\"), @io.string + end end end