Permalink
Browse files

use own default logger for Faraday

  • Loading branch information...
1 parent 81d2dce commit cc27363e6637a7f74e7962551d43855d1bee6ff9 shutl committed Aug 19, 2013
@@ -1,65 +1,67 @@
require 'forwardable'
-module Shutl::Resource
- class DefaultLogger < Faraday::Middleware
- extend Forwardable
+module Faraday
+ class Middleware
+ class DefaultLogger < Faraday::Middleware
+ extend Forwardable
- def_delegators :@logger, :debug, :info, :warn, :error, :fatal
+ def_delegators :@logger, :debug, :info, :warn, :error, :fatal
- def initialize(app, options = {})
- @app = app
- @logger = options.fetch(:logger) {
- require 'logger'
- ::Logger.new($stderr)
- }
- end
+ def initialize(app, options = {})
+ @app = app
+ @logger = options.fetch(:logger) {
+ require 'logger'
+ ::Logger.new($stderr)
+ }
+ end
- def call(env)
- start_time = Time.now
- info request_info(env)
- debug request_debug(env)
+ def call(env)
+ start_time = Time.now
+ info request_info(env)
+ debug request_debug(env)
- @app.call(env).on_complete do
- end_time = Time.now
- response_time = end_time - start_time
- info response_info(env, response_time)
- debug response_debug(env)
+ @app.call(env).on_complete do
+ end_time = Time.now
+ response_time = end_time - start_time
+ info response_info(env, response_time)
+ debug response_debug(env)
+ end
end
- end
- private
+ private
- def request_info(env)
- "Started %s request to: %s" % [ env[:method].to_s.upcase, env[:url] ]
- end
+ def request_info(env)
+ "Started %s request to: %s" % [ env[:method].to_s.upcase, env[:url] ]
+ end
- def response_info(env, response_time)
- "Response from %s; Status: %d; Time: %.1fms" % [ env[:url], env[:status], (response_time * 1_000.0) ]
- end
+ def response_info(env, response_time)
+ "Response from %s; Status: %d; Time: %.1fms" % [ env[:url], env[:status], (response_time * 1_000.0) ]
+ end
- def request_debug(env)
- debug_message("Request", env[:request_headers], env[:body])
- end
+ def request_debug(env)
+ debug_message("Request", env[:request_headers], env[:body])
+ end
- def response_debug(env)
- debug_message("Response", env[:response_headers], env[:body])
- end
+ def response_debug(env)
+ debug_message("Response", env[:response_headers], env[:body])
+ end
- def debug_message(name, headers, body)
- <<-MESSAGE.gsub(/^ +([^ ])/m, '\\1')
- #{name} Headers:
+ def debug_message(name, headers, body)
+ <<-MESSAGE.gsub(/^ +([^ ])/m, '\\1')
+ #{name} Headers:
----------------
- #{format_headers(headers)}
+ #{format_headers(headers)}
- #{name} Body:
+ #{name} Body:
-------------
- #{body}
- MESSAGE
- end
+ #{body}
+ MESSAGE
+ end
- def format_headers(headers)
- length = headers.map {|k,v| k.to_s.size }.max
- headers.map { |name, value| "#{name.to_s.ljust(length)} : #{value}" }.join("\n")
+ def format_headers(headers)
+ length = headers.map {|k,v| k.to_s.size }.max
+ headers.map { |name, value| "#{name.to_s.ljust(length)} : #{value}" }.join("\n")
+ end
end
end
end
@@ -13,7 +13,7 @@ def connection
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
if Shutl::Resource.logger
- faraday.use :extended_logging, logger: Shutl::Resource.logger
+ faraday.use :default_logger, logger: Shutl::Resource.logger
end
end
end
@@ -1,5 +1,5 @@
module Shutl
module Resource
- VERSION = '1.1.0'
+ VERSION = '1.1.1'
end
end
View
@@ -7,10 +7,14 @@ module Shutl
require 'shutl_auth'
require 'shutl/resource/version'
require 'shutl/resource/configuration'
+require 'shutl/resource/default_logger'
require 'shutl/resource/rest'
require 'shutl/resource/rest_class_methods'
require 'shutl/resource/errors'
+Faraday::Middleware.register_middleware :default_logger => :DefaultLogger
+
+
module Shutl::Resource
extend self

0 comments on commit cc27363

Please sign in to comment.