Permalink
Browse files

add logger adapter

  • Loading branch information...
1 parent fa8c3f1 commit 1507503f6823ceefa589c50d3edcdd44d7cf70b9 @technoweenie technoweenie committed Feb 19, 2011
Showing with 71 additions and 2 deletions.
  1. +4 −2 lib/faraday/adapter.rb
  2. +30 −0 lib/faraday/adapter/logger.rb
  3. +37 −0 test/adapters/logger_test.rb
View
@@ -13,7 +13,8 @@ class Adapter < Middleware
:EMSynchrony => 'em_synchrony',
:Patron => 'patron',
:Excon => 'excon',
- :Test => 'test'
+ :Test => 'test',
+ :Logger => 'logger'
register_lookup_modules \
:action_dispatch => :ActionDispatch,
@@ -22,7 +23,8 @@ class Adapter < Middleware
:typhoeus => :Typhoeus,
:patron => :Patron,
:em_synchrony => :EMSynchrony,
- :excon => :Excon
+ :excon => :Excon,
+ :logger => :Logger
def call(env)
process_body_for_request(env)
@@ -0,0 +1,30 @@
+module Faraday
+ class Adapter
+ class Logger < Faraday::Adapter
+ def initialize(app = nil, logger = nil)
+ super(app)
+ @logger = logger || begin
+ require 'logger'
+ Logger.new(STDOUT)
+ end
+ end
+
+ def call(env)
+ super
+ @logger.info "#{env[:method]} #{env[:url].to_s}"
+ @logger.debug("request") do
+ env[:request_headers].map { |(k, v)| "#{k}: #{v.inspect}" }.join("\n")
+ end
+
+ env[:response].on_complete do |resp_env|
+ @logger.info("Status") { env[:status].to_s }
+ @logger.debug("response") do
+ resp_env[:response_headers].map { |(k, v)| "#{k}: #{v.inspect}" }.join("\n")
+ end
+ end
+
+ @app.call(env)
+ end
+ end
+ end
+end
@@ -0,0 +1,37 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
+require 'stringio'
+require 'logger'
+
+module Adapters
+ class LoggerTest < Faraday::TestCase
+ def setup
+ @io = StringIO.new
+ @logger = Logger.new(@io)
+ @logger.level = Logger::DEBUG
+
+ @conn = Faraday::Connection.new do |b|
+ b.adapter :logger, @logger
+ b.adapter :test do |stubs|
+ stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
+ end
+ end
+ @resp = @conn.get '/hello', :accept => 'text/html'
+ end
+
+ def test_still_returns_output
+ assert_equal 'hello', @resp.body
+ end
+
+ def test_logs_method_and_url
+ assert_match "get /hello", @io.string
+ end
+
+ def test_logs_request_headers
+ assert_match %(Accept: "text/html), @io.string
+ end
+
+ def test_logs_response_headers
+ assert_match %(Content-Type: "text/html), @io.string
+ end
+ end
+end

0 comments on commit 1507503

Please sign in to comment.