mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-06 00:03:36 -04:00
added options hash to Logger to support logging bodies
This commit is contained in:
parent
0e5c87ee43
commit
fb6ed87714
@ -4,12 +4,15 @@ module Faraday
|
|||||||
class Response::Logger < Response::Middleware
|
class Response::Logger < Response::Middleware
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
|
|
||||||
def initialize(app, logger = nil)
|
DEFAULT_OPTIONS = { :bodies => false }
|
||||||
|
|
||||||
|
def initialize(app, logger = nil, options = {})
|
||||||
super(app)
|
super(app)
|
||||||
@logger = logger || begin
|
@logger = logger || begin
|
||||||
require 'logger'
|
require 'logger'
|
||||||
::Logger.new(STDOUT)
|
::Logger.new(STDOUT)
|
||||||
end
|
end
|
||||||
|
@options = DEFAULT_OPTIONS.merge(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def_delegators :@logger, :debug, :info, :warn, :error, :fatal
|
def_delegators :@logger, :debug, :info, :warn, :error, :fatal
|
||||||
@ -17,13 +20,14 @@ module Faraday
|
|||||||
def call(env)
|
def call(env)
|
||||||
info "#{env.method} #{env.url.to_s}"
|
info "#{env.method} #{env.url.to_s}"
|
||||||
debug('request') { dump_headers env.request_headers }
|
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
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_complete(env)
|
def on_complete(env)
|
||||||
info('Status') { env.status.to_s }
|
info('Status') { env.status.to_s }
|
||||||
debug('response') { dump_headers env.response_headers }
|
debug('response') { dump_headers env.response_headers }
|
||||||
|
debug('response') { dump_body env[:body] } if env[:body] && log_body?(:response)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@ -31,5 +35,21 @@ module Faraday
|
|||||||
def dump_headers(headers)
|
def dump_headers(headers)
|
||||||
headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
|
headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
@ -4,18 +4,28 @@ require 'logger'
|
|||||||
|
|
||||||
module Adapters
|
module Adapters
|
||||||
class LoggerTest < Faraday::TestCase
|
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
|
def setup
|
||||||
@io = StringIO.new
|
@io = StringIO.new
|
||||||
@logger = Logger.new(@io)
|
@logger = Logger.new(@io)
|
||||||
@logger.level = Logger::DEBUG
|
@logger.level = Logger::DEBUG
|
||||||
|
|
||||||
@conn = Faraday.new do |b|
|
@conn = conn(@logger)
|
||||||
b.response :logger, @logger
|
@resp = @conn.get '/hello', nil, :accept => 'text/html'
|
||||||
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'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_still_returns_output
|
def test_still_returns_output
|
||||||
@ -23,19 +33,50 @@ module Adapters
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_logs_method_and_url
|
def test_logs_method_and_url
|
||||||
assert_match "post http:/hello", @io.string
|
assert_match "get http:/hello", @io.string
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_logs_request_headers
|
def test_logs_request_headers
|
||||||
assert_match %(Accept: "text/html), @io.string
|
assert_match %(Accept: "text/html), @io.string
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_logs_request_body
|
|
||||||
assert_match %(name=Unagi), @io.string
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_logs_response_headers
|
def test_logs_response_headers
|
||||||
assert_match %(Content-Type: "text/html), @io.string
|
assert_match %(Content-Type: "text/html), @io.string
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user