Issue 140 - Implement Logger for Koala::Utils #177

Merged
merged 2 commits into from Apr 1, 2012
@@ -72,6 +72,9 @@ def self.make_request(path, args, verb, options = {})
conn = Faraday.new(server(request_options), request_options, &(faraday_middleware || DEFAULT_MIDDLEWARE))
response = conn.send(verb, path, (verb == "post" ? params : {}))
+
+ # Log URL information
+ Koala::Utils.debug "#{verb.upcase}: #{path} params: #{params}"
Koala::HTTPService::Response.new(response.status.to_i, response.body, response.headers)
end
View
@@ -1,12 +1,26 @@
module Koala
module Utils
+
+ # Add a logger
+ # Uses the ruby logger as default
+ require 'logger'
+ require 'forwardable'
+ extend Forwardable
+ extend self
+
+ def_delegators :logger, :debug, :info, :warn, :error, :fatal, :level, :level=
+
+ attr_accessor :logger
+ self.logger = Logger.new(STDOUT)
+ self.logger.level = Logger::ERROR
+
# @private
DEPRECATION_PREFIX = "KOALA: Deprecation warning: "
# Prints a deprecation message.
# Each individual message will only be printed once to avoid spamming.
- def self.deprecate(message)
+ def deprecate(message)
@posted_deprecations ||= []
unless @posted_deprecations.include?(message)
# only include each message once
@@ -244,6 +244,13 @@
@mock_connection.should_receive(:get).with(anything, {}).and_return(@mock_http_response)
Koala::HTTPService.make_request("anything", args, "get")
end
+
+ it "logs verb, url and params to debug" do
+ args = {"a" => :b, "c" => 3}
+ log_message = "GET: anything params: #{args}"
+ Koala::Utils.logger.should_receive(:debug).with(log_message)
+ Koala::HTTPService.make_request("anything", args, "get")
+ end
end
describe "for POSTs" do
@@ -262,6 +269,13 @@
@mock_connection.should_receive(:post).with(anything, hash_including("source" => upload_io)).and_return(@mock_http_response)
Koala::HTTPService.make_request("anything", {:source => u}, "post")
end
+
+ it "logs verb, url and params to debug" do
+ args = {"a" => :b, "c" => 3}
+ log_message = "POST: anything params: #{args}"
+ Koala::Utils.logger.should_receive(:debug).with(log_message)
+ Koala::HTTPService.make_request("anything", args, "post")
+ end
end
end
View
@@ -31,4 +31,25 @@
Koala::Utils.deprecate(message)
end
end
+
+ describe ".logger" do
+ it "has an accessor for logger" do
+ Koala::Utils.methods.map(&:to_sym).should include(:logger)
+ Koala::Utils.methods.map(&:to_sym).should include(:logger=)
+ end
+
+ it "defaults to the standard ruby logger with level set to ERROR" do |variable|
+ Koala::Utils.logger.should be_kind_of(Logger)
+ Koala::Utils.logger.level.should == Logger::ERROR
+ end
+
+ logger_methods = [:debug, :info, :warn, :error, :fatal]
+
+ logger_methods.each do |logger_method|
+ it "should delegate #{logger_method} to the attached logger" do
+ Koala::Utils.logger.should_receive(logger_method)
+ Koala::Utils.send(logger_method, "Test #{logger_method} message")
+ end
+ end
+ end
end