summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel DeLeo <dan@opscode.com>2011-03-15 19:50:45 -0700
committerDaniel DeLeo <dan@opscode.com>2011-03-15 19:50:45 -0700
commit460b9121f09ee04a5a493c45ec18f83f97ff0b61 (patch)
treec8bd6e18ea0ffe63a631f9f85ff21494cc7a2e5b
parent333a74a64860df896b915559570f70a4b8f40683 (diff)
parent71bc3cda5d43b6c2548d710fc3bc133f7d8c1de0 (diff)
downloadchef-460b9121f09ee04a5a493c45ec18f83f97ff0b61.tar.gz
Merge branch 'CHEF-2033'
-rw-r--r--chef/chef.gemspec2
-rw-r--r--chef/lib/chef/application.rb11
-rw-r--r--chef/lib/chef/application/client.rb4
-rw-r--r--chef/lib/chef/log.rb59
-rw-r--r--chef/lib/chef/rest.rb9
-rw-r--r--chef/spec/unit/knife/cookbook_site_share_spec.rb2
-rw-r--r--chef/spec/unit/rest_spec.rb31
7 files changed, 32 insertions, 86 deletions
diff --git a/chef/chef.gemspec b/chef/chef.gemspec
index 3fec1c9ba9..2968a6cb97 100644
--- a/chef/chef.gemspec
+++ b/chef/chef.gemspec
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
s.add_dependency "mixlib-config", ">= 1.1.2"
s.add_dependency "mixlib-cli", ">= 1.1.0"
- s.add_dependency "mixlib-log", ">= 1.2.0"
+ s.add_dependency "mixlib-log", ">= 1.3.0"
s.add_dependency "mixlib-authentication", ">= 1.1.0"
s.add_dependency "ohai", ">= 0.5.7"
diff --git a/chef/lib/chef/application.rb b/chef/lib/chef/application.rb
index 7a66a7a34c..d821760b04 100644
--- a/chef/lib/chef/application.rb
+++ b/chef/lib/chef/application.rb
@@ -86,9 +86,18 @@ class Chef::Application
end
- # Initialize and configure the logger
+ # Initialize and configure the logger. If the configured log location is not
+ # STDOUT, but stdout is a TTY and we're not daemonizing, we set up a secondary
+ # logger with output to stdout. This way, we magically do the right thing when
+ # the user has configured logging to a file but they're running chef in the
+ # shell to debug something.
def configure_logging
Chef::Log.init(Chef::Config[:log_location])
+ if ( Chef::Config[:log_location] != STDOUT ) && STDOUT.tty? && (!Chef::Config[:daemonize])
+ stdout_logger = Logger.new(STDOUT)
+ stdout_logger.formatter = Chef::Log.logger.formatter
+ Chef::Log.loggers << stdout_logger
+ end
Chef::Log.level = Chef::Config[:log_level]
end
diff --git a/chef/lib/chef/application/client.rb b/chef/lib/chef/application/client.rb
index 221bf28ed0..250f092c8b 100644
--- a/chef/lib/chef/application/client.rb
+++ b/chef/lib/chef/application/client.rb
@@ -193,8 +193,8 @@ class Chef::Application::Client < Chef::Application
def configure_logging
super
- Chef::Log.verbose = Chef::Config[:verbose_logging]
- Mixlib::Authentication::Log.logger = Ohai::Log.logger = Chef::Log.logger
+ Mixlib::Authentication::Log.use_log_devices( Chef::Log )
+ Ohai::Log.use_log_devices( Chef::Log )
end
def setup_application
diff --git a/chef/lib/chef/log.rb b/chef/lib/chef/log.rb
index f582cd895b..7355ec7574 100644
--- a/chef/lib/chef/log.rb
+++ b/chef/lib/chef/log.rb
@@ -8,9 +8,9 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -23,66 +23,17 @@ require 'mixlib/log'
class Chef
class Log
extend Mixlib::Log
-
- class << self
- attr_accessor :verbose
- attr_reader :verbose_logger
- protected :verbose_logger
-
- def verbose
- !(@verbose_logger.nil?)
- end
-
- def verbose=(value)
- if value
- @verbose_logger ||= Logger.new(STDOUT)
- @verbose_logger.level = self.logger.level
- @verbose_logger.formatter = self.logger.formatter
- else
- @verbose_logger = nil
- end
- self.verbose
- end
-
- [:debug, :info, :warn, :error, :fatal].each do |method_name|
- class_eval(<<-METHOD_DEFN, __FILE__, __LINE__)
- def #{method_name}(msg=nil, &block)
- @logger.#{method_name}(msg, &block)
- end
- METHOD_DEFN
- end
-
- [:debug?, :info?, :warn?, :error?, :fatal?].each do |method_name|
- class_eval(<<-METHOD_DEFN, __FILE__, __LINE__)
- def #{method_name}
- @logger.#{method_name}
- end
- METHOD_DEFN
- end
-
- def <<(msg)
- @logger << msg
- end
-
- def add(severity, message = nil, progname = nil, &block)
- @logger.add(severity, message = nil, progname = nil, &block)
- end
-
- end
- # NOTE: Mixlib::Log initially sets @logger to nil and depends on
- # #init being called to initialize the logger. We don't want to
- # incur extra method call overhead for every log message so we're
- # accessing the logger by instance variable, which means we need to
- # make Mixlib::Log initialize it.
+ # Force initialization of the primary log device (@logger)
init
+
class Formatter
def self.show_time=(*args)
Mixlib::Log::Formatter.show_time = *args
end
end
-
+
end
end
diff --git a/chef/lib/chef/rest.rb b/chef/lib/chef/rest.rb
index 03a0b6f866..24a3cb4fe5 100644
--- a/chef/lib/chef/rest.rb
+++ b/chef/lib/chef/rest.rb
@@ -374,15 +374,6 @@ class Chef
response.read_body do |chunk|
tf.write(chunk)
size += chunk.size
- if Chef::Log.verbose
- if size == 0
- Chef::Log.debug("#{url.path} done (0 length file)")
- elsif total == 0
- Chef::Log.debug("#{url.path} (zero content length or no Content-Length header)")
- else
- Chef::Log.debug("#{url.path}" + " %d%% done (%d of %d)" % [(size * 100) / total, size, total])
- end
- end
end
tf.close
tf
diff --git a/chef/spec/unit/knife/cookbook_site_share_spec.rb b/chef/spec/unit/knife/cookbook_site_share_spec.rb
index b58f37347f..130be5edb5 100644
--- a/chef/spec/unit/knife/cookbook_site_share_spec.rb
+++ b/chef/spec/unit/knife/cookbook_site_share_spec.rb
@@ -95,7 +95,7 @@ describe Chef::Knife::CookbookSiteShare do
Chef::CookbookSiteStreamingUploader.stub!(:post).and_return(@upload_response)
@log = StringIO.new
- Chef::Log.logger = Logger.new(@log)
+ Chef::Log.init(@log)
File.stub(:open).and_return(true)
end
diff --git a/chef/spec/unit/rest_spec.rb b/chef/spec/unit/rest_spec.rb
index c50cf9f2cc..b2d4072299 100644
--- a/chef/spec/unit/rest_spec.rb
+++ b/chef/spec/unit/rest_spec.rb
@@ -55,10 +55,8 @@ Y6S6MeZ69Rp89ma4ttMZ+kwi1+XyHqC/dlcVRW42Zl5Dc7BALRlJjQ==
describe Chef::REST do
before(:each) do
@log_stringio = StringIO.new
- @logger = Logger.new(@log_stringio)
- @original_chef_logger = Chef::Log.logger
- Chef::Log.logger = @logger
-
+ Chef::Log.init(@log_stringio)
+
Chef::REST::CookieJar.stub!(:instance).and_return({})
@base_url = "http://chef.example.com:4000"
@monkey_uri = URI.parse("http://chef.example.com:4000/monkey")
@@ -66,10 +64,7 @@ describe Chef::REST do
Chef::REST::CookieJar.instance.clear
end
-
- after do
- Chef::Log.logger = @original_chef_logger
- end
+
describe "calling an HTTP verb on a path or absolute URL" do
it "adds a relative URL to the base url it was initialized with" do
@@ -212,7 +207,7 @@ describe Chef::REST do
http_response = Net::HTTPFound.new("1.1", "302", "bob is taking care of that one for me today")
http_response.add_field("location", @url.path)
http_response.stub!(:read_body)
-
+
@http_client.stub!(:request).and_yield(http_response).and_return(http_response)
lambda { @rest.run_request(:GET, @url) }.should raise_error(Chef::Exceptions::RedirectLimitExceeded)
end
@@ -232,7 +227,7 @@ describe Chef::REST do
http_response.stub!(:read_body)
@http_client.stub!(:request).and_yield(http_response).and_return(http_response)
lambda {@rest.run_request(:GET, @url)}.should raise_error(Net::HTTPFatalError)
- @log_stringio.string.should match(Regexp.escape('WARN -- : HTTP Request Returned 500 drooling from inside of mouth: Ears get sore!, Not even four'))
+ @log_stringio.string.should match(Regexp.escape('WARN: HTTP Request Returned 500 drooling from inside of mouth: Ears get sore!, Not even four'))
end
it "should raise an exception on an unsuccessful request" do
@@ -363,9 +358,9 @@ describe Chef::REST do
http_response = Net::HTTPFound.new("1.1", "302", "bob is taking care of that one for me today")
http_response.add_field("location", @url.path)
http_response.stub!(:read_body)
-
+
@http_client.stub!(:request).and_yield(http_response).and_return(http_response)
-
+
lambda { @rest.api_request(:GET, @url) }.should raise_error(Chef::Exceptions::RedirectLimitExceeded)
end
@@ -374,7 +369,7 @@ describe Chef::REST do
http_response.add_field("location", @url.path)
http_response.stub!(:read_body)
@http_client.stub!(:request).and_yield(http_response).and_return(http_response)
-
+
lambda { @rest.api_request(:GET, @url) }.should raise_error(Chef::Exceptions::RedirectLimitExceeded)
end
@@ -384,9 +379,9 @@ describe Chef::REST do
http_response.stub!(:body).and_return('{ "error":[ "Ears get sore!", "Not even four" ] }')
http_response.stub!(:read_body)
@http_client.stub!(:request).and_yield(http_response).and_return(http_response)
-
+
lambda {@rest.run_request(:GET, @url)}.should raise_error(Net::HTTPFatalError)
- @log_stringio.string.should match(Regexp.escape('WARN -- : HTTP Request Returned 500 drooling from inside of mouth: Ears get sore!, Not even four'))
+ @log_stringio.string.should match(Regexp.escape('WARN: HTTP Request Returned 500 drooling from inside of mouth: Ears get sore!, Not even four'))
end
it "should raise an exception on an unsuccessful request" do
@@ -478,11 +473,11 @@ describe Chef::REST do
tempfile = mock("die", :path => "/tmp/ragefist", :close => true)
tempfile.should_receive(:close!).at_least(2).times
Tempfile.stub!(:new).with("chef-rest").and_return(tempfile)
-
+
http_response = Net::HTTPFound.new("1.1", "302", "bob is taking care of that one for me today")
http_response.add_field("location", @url.path)
http_response.stub!(:read_body)
-
+
@http_client.stub!(:request).and_yield(http_response).and_yield(@http_response).and_return(http_response, @http_response)
@rest.fetch("cookbooks/a_cookbook") {|tmpfile| "shouldn't get here"}
end
@@ -491,7 +486,7 @@ describe Chef::REST do
http_response = Net::HTTPFound.new("1.1", "302", "bob is taking care of that one for me today")
http_response.add_field("location","/that-thing-is-here-now")
http_response.stub!(:read_body)
-
+
block_called = false
@http_client.stub!(:request).and_yield(@http_response).and_return(http_response, @http_response)
@rest.fetch("cookbooks/a_cookbook") do |tmpfile|