summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Running <jr@getchef.com>2016-03-24 12:01:40 -0500
committerJordan Running <jr@getchef.com>2016-03-29 10:05:34 -0500
commit7d505ce8101679e744692b00627ef015dd8a153f (patch)
tree640d184fb9fc5777b7f610a9c9323e9a26884a2e
parentbd272751f4493b4414802490b068dd0196fabf02 (diff)
downloadchef-7d505ce8101679e744692b00627ef015dd8a153f.tar.gz
Add shorthand :syslog and :win_evt for log_location configjr/logger-config
-rw-r--r--RELEASE_NOTES.md9
-rw-r--r--lib/chef/application.rb15
-rw-r--r--spec/unit/application_spec.rb27
3 files changed, 51 insertions, 0 deletions
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 6f67aa7b09..0d7ce17261 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -36,3 +36,12 @@ from Chef::Provider when mixing `use_inline_resources` into classes that only in
class. If any code has been written like this, it should be modified to correctly inherit from
Chef::Provider::LWRPBase instead (which will have the side effect of fixing it so that it correctly works
on Chef 11.0-12.5 as well).
+
+## Shorthand options for `log_location`
+
+The `log_location` setting now accepts shorthand `:syslog` and
+`:win_evt` options. `:syslog` is shorthand for `Chef::Log::Syslog.new`
+and `:win_evt` is shorthand for `Chef::Log::WinEvt.new`. All previously
+valid options are still valid, including Logger or Logger-like
+instances, e.g. `Chef::Log::Syslog.new` with other args than the
+defaults.
diff --git a/lib/chef/application.rb b/lib/chef/application.rb
index cd3e7f8c24..7dbffd8dec 100644
--- a/lib/chef/application.rb
+++ b/lib/chef/application.rb
@@ -141,6 +141,7 @@ class Chef
# that a user has configured a log_location in client.rb, but is running
# chef-client by hand to troubleshoot a problem.
def configure_logging
+ configure_log_location
Chef::Log.init(MonoLogger.new(Chef::Config[:log_location]))
if want_additional_logger?
configure_stdout_logger
@@ -151,6 +152,20 @@ class Chef
Chef::Application.fatal!("Aborting due to invalid 'log_location' configuration", 2)
end
+ # Turn `log_location :syslog` and `log_location :win_evt` into the
+ # appropriate loggers.
+ def configure_log_location
+ log_location = Chef::Config[:log_location]
+ return unless log_location.respond_to?(:to_sym)
+
+ Chef::Config[:log_location] =
+ case log_location.to_sym
+ when :syslog then Chef::Log::Syslog.new
+ when :win_evt then Chef::Log::WinEvt.new
+ else log_location # Probably a path; let MonoLogger sort it out
+ end
+ end
+
def configure_stdout_logger
stdout_logger = MonoLogger.new(STDOUT)
stdout_logger.formatter = Chef::Log.logger.formatter
diff --git a/spec/unit/application_spec.rb b/spec/unit/application_spec.rb
index 462b7314c4..8ab6e13204 100644
--- a/spec/unit/application_spec.rb
+++ b/spec/unit/application_spec.rb
@@ -247,6 +247,33 @@ describe Chef::Application do
it_behaves_like "log_level_is_auto"
end
+
+ describe "log_location" do
+ shared_examples("sets log_location") do |config_value, expected_class|
+ context "when the configured value is #{config_value.inspect}" do
+ let(:logger_instance) { instance_double(expected_class).as_null_object }
+
+ before do
+ allow(expected_class).to receive(:new).and_return(logger_instance)
+ Chef::Config[:log_location] = config_value
+ end
+
+ it "it sets log_location to an instance of #{expected_class}" do
+ expect(expected_class).to receive(:new).with no_args
+ @app.configure_logging
+ expect(Chef::Config[:log_location]).to be logger_instance
+ end
+ end
+ end
+
+ if Chef::Platform.windows?
+ it_behaves_like "sets log_location", :win_evt, Chef::Log::WinEvt
+ it_behaves_like "sets log_location", "win_evt", Chef::Log::WinEvt
+ else
+ it_behaves_like "sets log_location", :syslog, Chef::Log::Syslog
+ it_behaves_like "sets log_location", "syslog", Chef::Log::Syslog
+ end
+ end
end
end