summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Hain <shain@getchef.com>2014-07-17 09:08:45 -0700
committerScott Hain <shain@getchef.com>2014-07-17 09:09:20 -0700
commit62718f9dfde8dfdae1454d85b134c4c49d86428a (patch)
tree6ee1b0990f4c7e7582dc6b81b4d99aae572fb941
parent83b159e0dc46fd5ed241a43536ef4e7cebbbdcd4 (diff)
downloadchef-62718f9dfde8dfdae1454d85b134c4c49d86428a.tar.gz
Logger framework poc
-rw-r--r--lib/chef/application.rb35
-rw-r--r--lib/chef/config.rb6
-rw-r--r--lib/chef/log.rb1
-rw-r--r--lib/chef/loggers/chef_logger.rb96
-rw-r--r--lib/chef/monologger.rb1
5 files changed, 129 insertions, 10 deletions
diff --git a/lib/chef/application.rb b/lib/chef/application.rb
index 5b404a3a50..bce7c95df3 100644
--- a/lib/chef/application.rb
+++ b/lib/chef/application.rb
@@ -32,8 +32,8 @@ class Chef::Application
include Mixlib::CLI
def initialize
+ puts "YAY"
super
-
@chef_client = nil
@chef_client_json = nil
@@ -121,14 +121,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
- Chef::Log.init(MonoLogger.new(Chef::Config[:log_location]))
- if want_additional_logger?
- configure_stdout_logger
+ 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
+ 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!"
+ 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]}"
+ end
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
def configure_stdout_logger
diff --git a/lib/chef/config.rb b/lib/chef/config.rb
index 65952b8cf7..1dff1f8845 100644
--- a/lib/chef/config.rb
+++ b/lib/chef/config.rb
@@ -300,6 +300,12 @@ 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])
+ end
+
+ default :loggers, []
+
# Using `force_formatter` causes chef to default to formatter output when STDOUT is not a tty
default :force_formatter, false
diff --git a/lib/chef/log.rb b/lib/chef/log.rb
index 131d706a5e..2c40a01438 100644
--- a/lib/chef/log.rb
+++ b/lib/chef/log.rb
@@ -36,4 +36,3 @@ class Chef
end
end
-
diff --git a/lib/chef/loggers/chef_logger.rb b/lib/chef/loggers/chef_logger.rb
new file mode 100644
index 0000000000..920e6bb1fb
--- /dev/null
+++ b/lib/chef/loggers/chef_logger.rb
@@ -0,0 +1,96 @@
+require 'logger'
+
+require 'pp'
+
+#== ChefLogger
+# A subclass of Ruby's stdlib Logger with all the mutex and logrotation stuff
+# ripped out.
+
+class Chef
+
+ module Loggers
+ class ChefLogger < Logger
+
+ #
+ # === Synopsis
+ #
+ # Logger.new(name, shift_age = 7, shift_size = 1048576)
+ # Logger.new(name, shift_age = 'weekly')
+ #
+ # === Args
+ #
+ # +logdev+::
+ # The log device. This is a filename (String) or IO object (typically
+ # +STDOUT+, +STDERR+, or an open file).
+ # +shift_age+::
+ # Number of old log files to keep, *or* frequency of rotation (+daily+,
+ # +weekly+ or +monthly+).
+ # +shift_size+::
+ # Maximum logfile size (only applies when +shift_age+ is a number).
+ #
+ # === Description
+ #
+ # Create an instance.
+ #
+ def initialize(args)
+ unless args[:log_location].nil?
+ @progname = nil
+ @level = DEBUG
+ @default_formatter = Formatter.new
+ @formatter = nil
+ @logdev = nil
+ unless args[:log_location].nil?
+ @logdev = LocklessLogDevice.new(args[:log_location])
+ end
+ end
+ end
+
+ class LocklessLogDevice < 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)
+ puts "In write"
+ @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
diff --git a/lib/chef/monologger.rb b/lib/chef/monologger.rb
index 464b21bdd3..f65e9535b4 100644
--- a/lib/chef/monologger.rb
+++ b/lib/chef/monologger.rb
@@ -89,4 +89,3 @@ class MonoLogger < Logger
end
-