forked from lostisland/faraday
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa8c3f1
commit 1507503
Showing
3 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |