diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2015-05-06 15:46:05 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2015-05-06 15:46:05 -0700 |
commit | e4bf480137d5e06aba7c9fee4ae1577361a44a47 (patch) | |
tree | d8bd01986aa871eb671eb79e26d05e27bd379a3d | |
parent | 460a06a241237972ac602f470a558e18bdef595f (diff) | |
parent | 4399a210e499b0693d31322038def48384805e08 (diff) | |
download | chef-e4bf480137d5e06aba7c9fee4ae1577361a44a47.tar.gz |
Merge pull request #3262 from chef/lcg/add-chef-log-syslog
add Chef::Log::Syslog class
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | chef.gemspec | 2 | ||||
-rw-r--r-- | lib/chef/log.rb | 1 | ||||
-rw-r--r-- | lib/chef/log/syslog.rb | 50 | ||||
-rw-r--r-- | spec/unit/log/syslog_spec.rb | 53 |
5 files changed, 108 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 7078b9e676..bad8bffd37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ Ensure suid bit is preserved if group or owner changes * [**Tim Smith**](https://github.com/tas50): Convert wiki links to point to docs.chef.io +* [**SAWANOBORI Yukihiko**](https://github.com/sawanoboly): + Add Chef::Log::Syslog class for integrating sending logs to syslog * Add an integration test of chef-client with empty ENV. #3321 * Switch over Windows builds to universal builds. #3278 diff --git a/chef.gemspec b/chef.gemspec index ec600f1f89..7cf6380062 100644 --- a/chef.gemspec +++ b/chef.gemspec @@ -41,6 +41,8 @@ Gem::Specification.new do |s| s.add_dependency "serverspec", "~> 2.7" s.add_dependency "specinfra", "~> 2.10" + s.add_dependency "syslog-logger", "~> 1.6" + s.add_development_dependency "rack" # Rake 10.2 drops Ruby 1.8 support diff --git a/lib/chef/log.rb b/lib/chef/log.rb index 682afcea4b..a47002a2fc 100644 --- a/lib/chef/log.rb +++ b/lib/chef/log.rb @@ -21,6 +21,7 @@ require 'logger' require 'chef/monologger' require 'chef/exceptions' require 'mixlib/log' +require 'chef/log/syslog' unless (RUBY_PLATFORM =~ /mswin|mingw|windows/) class Chef class Log diff --git a/lib/chef/log/syslog.rb b/lib/chef/log/syslog.rb new file mode 100644 index 0000000000..fcb3c972e8 --- /dev/null +++ b/lib/chef/log/syslog.rb @@ -0,0 +1,50 @@ +# +# Author:: Lamont Granquist (<lamont@chef.io>) +# Author:: SAWANOBORI Yukihiko (<sawanoboriyu@higanworks.com>) +# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# 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. +# See the License for the specific language governing permissions and +# limitations under the License. + +require 'logger' +require 'syslog-logger' + +class Chef + class Log + # + # Chef::Log::Syslog class. + # usage in client.rb: + # log_location Chef::Log::Syslog.new("chef-client", ::Syslog::LOG_DAEMON) + # + class Syslog < Logger::Syslog + attr_accessor :sync, :formatter + + def initialize(program_name = 'chef-client', facility = ::Syslog::LOG_DAEMON, logopts=nil) + super + return if defined? ::Logger::Syslog::SYSLOG + ::Logger::Syslog.const_set :SYSLOG, SYSLOG + end + + def write(message) + data = message.match(/(\[.+?\]) ([\w]+):(.*)$/) + self.send(data[2].downcase.to_sym, data[3].strip) + rescue NoMethodError + self.send(:info, message) + end + + def close + end + end + end +end + diff --git a/spec/unit/log/syslog_spec.rb b/spec/unit/log/syslog_spec.rb new file mode 100644 index 0000000000..3db90e50c6 --- /dev/null +++ b/spec/unit/log/syslog_spec.rb @@ -0,0 +1,53 @@ +# +# Author:: SAWANOBORI Yukihiko (<sawanoboriyu@higanworks.com>) +# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# 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. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' +require 'chef' + +describe "Chef::Log::Syslog", :unix_only => true do + let(:syslog) { Chef::Log::Syslog.new } + let(:app) { Chef::Application.new } + + before do + Chef::Log.init(MonoLogger.new(syslog)) + @old_log_level = Chef::Log.level + Chef::Log.level = :info + @old_loggers = Chef::Log.loggers + Chef::Log.use_log_devices([syslog]) + end + + after do + Chef::Log.level = @old_log_level + Chef::Log.use_log_devices(@old_loggers) + end + + it "should send message with severity info to syslog." do + expect(syslog).to receive(:info).with("*** Chef 12.4.0.dev.0 ***") + Chef::Log.info("*** Chef 12.4.0.dev.0 ***") + end + + it "should send message with severity warning to syslog." do + expect(syslog).to receive(:warn).with("No config file found or specified on command line, using command line options.") + Chef::Log.warn("No config file found or specified on command line, using command line options.") + end + + it "should fallback into send message with severity info to syslog when wrong format." do + expect(syslog).to receive(:info).with("chef message") + syslog.write("chef message") + end +end |