From 43dede749f71572f0ea3186bee1827ceea58bf6d Mon Sep 17 00:00:00 2001 From: Scott Hain Date: Fri, 18 Jul 2014 13:18:09 -0700 Subject: Some poc work done --- lib/chef/application.rb | 45 +++++++++++++-------------- lib/chef/config.rb | 4 +-- lib/chef/loggers/chef_logger.rb | 8 ++--- lib/chef/loggers/syslog_logger.rb | 65 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 31 deletions(-) create mode 100644 lib/chef/loggers/syslog_logger.rb diff --git a/lib/chef/application.rb b/lib/chef/application.rb index bce7c95df3..d052fcee01 100644 --- a/lib/chef/application.rb +++ b/lib/chef/application.rb @@ -32,7 +32,6 @@ class Chef::Application include Mixlib::CLI def initialize - puts "YAY" super @chef_client = nil @chef_client_json = nil @@ -73,7 +72,6 @@ class Chef::Application end end - # Parse configuration (options and config file) def configure_chef parse_options @@ -121,32 +119,33 @@ class Chef::Application # 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 - puts "LOGGERS! " + Chef::Config[:loggers].to_json - if Chef::Config[:loggers].nil? || Chef::Config[:loggers].empty? - begin - Chef::Log.init(MonoLogger.new(Chef::Config[:log_location])) - if want_additional_logger? - configure_stdout_logger + begin + Chef::Log.init(MonoLogger.new(Chef::Config[:log_location])) + if want_additional_logger? + configure_stdout_logger + end + Chef::Log.level = resolve_log_level + rescue StandardError => error + Chef::Log.fatal("Failed to open or create log file at #{Chef::Config[:log_location]}: #{error.class} (#{error.message})") + Chef::Application.fatal!("Aborting due to invalid 'log_location' configuration", 2) end - Chef::Log.level = resolve_log_level - rescue StandardError => error - Chef::Log.fatal("Failed to open or create log file at #{Chef::Config[:log_location]}: #{error.class} (#{error.message})") - Chef::Application.fatal!("Aborting due to invalid 'log_location' configuration", 2) - end else - puts "Create magic loggers!" + log_devices = Array.new Chef::Config[:loggers].each do |logger| - puts "Logger Name: " + logger[0] - puts "Logger type: " + logger[1] - puts "Log Level: " + logger[2].to_s - puts "Log init args: " + logger[3].to_json - - require "chef/loggers/chef_logger" - log_class = Chef::Loggers.const_get("#{logger[1]}".to_sym).new(logger[3]) - Chef::Log.init(log_class) or - raise StandardError, "No logger class found for #{logger[1]}" + begin + require_path = "chef/loggers/#{logger[0].gsub(/(.)([A-Z])/,'\1_\2').downcase}" + require require_path + log_class = Chef::Loggers.const_get("#{logger[0]}".to_sym).new(logger[1]) + log_devices << log_class + # rescue StandardError, LoadError => error + # Chef::Log.fatal("Failed to open or create logger of type #{logger[0]} with arguments: #{logger[1]} - (#{error.message})") + # Chef::Application.fatal!("Aborting due to invalid logger configuration", 2) + end end + + Chef::Log.use_log_devices(log_devices) + Chef::Log.level = resolve_log_level end end diff --git a/lib/chef/config.rb b/lib/chef/config.rb index 1dff1f8845..17aaad054b 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -300,8 +300,8 @@ class Chef # Logging location as either an IO stream or string representing log file path default :log_location, STDOUT - def self.add_logger(name, log_type, log_level=:info, args=nil) - loggers.push([name, log_type, log_level, args]) + def self.add_logger(log_type, args=nil) + loggers.push([log_type, args]) end default :loggers, [] diff --git a/lib/chef/loggers/chef_logger.rb b/lib/chef/loggers/chef_logger.rb index 920e6bb1fb..6b7c55cfb1 100644 --- a/lib/chef/loggers/chef_logger.rb +++ b/lib/chef/loggers/chef_logger.rb @@ -7,7 +7,6 @@ require 'pp' # ripped out. class Chef - module Loggers class ChefLogger < Logger @@ -33,20 +32,18 @@ class Chef # Create an instance. # def initialize(args) - unless args[:log_location].nil? @progname = nil @level = DEBUG @default_formatter = Formatter.new @formatter = nil - @logdev = nil + @logdev = STDOUT unless args[:log_location].nil? @logdev = LocklessLogDevice.new(args[:log_location]) end end end - class LocklessLogDevice < LogDevice - + class LocklessLogDevice < Logger::LogDevice def initialize(log = nil) @dev = @filename = @shift_age = @shift_size = nil if log.respond_to?(:write) and log.respond_to?(:close) @@ -59,7 +56,6 @@ class Chef end def write(message) - puts "In write" @dev.write(message) rescue Exception => ignored warn("log writing failed. #{ignored}") diff --git a/lib/chef/loggers/syslog_logger.rb b/lib/chef/loggers/syslog_logger.rb new file mode 100644 index 0000000000..e52a1bf847 --- /dev/null +++ b/lib/chef/loggers/syslog_logger.rb @@ -0,0 +1,65 @@ +require 'syslog' +require 'logger' + +class Chef + module Loggers + class SyslogLogger < Logger + + def initialize(args) + @progname = 'chef-client' + @level = DEBUG + @facility = SysLog::LOG_USER + @formatter = nil + @logdev = STDOUT + unless Syslog.opened? + @logdev = Syslog.open(progname, nil, facility) + end + end + end + + # class LocklessLogDevice < Logger::LogDevice + # def initialize(log = nil) + # @dev = @filename = @shift_age = @shift_size = nil + # if log.respond_to?(:write) and log.respond_to?(:close) + # @dev = log + # else + # @dev = open_logfile(log) + # @filename = log + # end + # @dev.sync = true + # end + # + # def write(message) + # @dev.write(message) + # rescue Exception => ignored + # warn("log writing failed. #{ignored}") + # end + # + # def close + # @dev.close rescue nil + # end + # + # private + # + # def open_logfile(filename) + # if (FileTest.exist?(filename)) + # open(filename, (File::WRONLY | File::APPEND)) + # else + # create_logfile(filename) + # end + # end + # + # def create_logfile(filename) + # logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT)) + # add_log_header(logdev) + # logdev + # end + # + # def add_log_header(file) + # file.write( + # "# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName] + # ) + # end + # end + end +end -- cgit v1.2.1