summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAJ Christensen <aj@opscode.com>2009-03-11 13:32:10 -0700
committerAJ Christensen <aj@junglist.gen.nz>2009-05-12 16:08:08 +1200
commitdcb2f0f89e0f19bfd82f890e359d28e476f784f6 (patch)
tree0dc771c6235d08d780db6f759f0ebfc124de9b5d /lib
parenta136f9823b8b0fa01da4e758e64c89bbab0036dc (diff)
downloadmixlib-log-dcb2f0f89e0f19bfd82f890e359d28e476f784f6.tar.gz
Convert to jeweler
Diffstat (limited to 'lib')
-rw-r--r--lib/mixlib/log.rb46
1 files changed, 18 insertions, 28 deletions
diff --git a/lib/mixlib/log.rb b/lib/mixlib/log.rb
index 1f64c5a..03b08b0 100644
--- a/lib/mixlib/log.rb
+++ b/lib/mixlib/log.rb
@@ -23,7 +23,15 @@ module Mixlib
@logger = nil
- attr_accessor :logger #:nodoc
+ attr_writer :logger #:nodoc
+
+ ##
+ # init always returns a configured logger
+ # and creates a new one if it doesn't yet exist
+ ##
+ def logger
+ init
+ end
# Use Mixlib::Log.init when you want to set up the logger manually. Arguments to this method
# get passed directly to Logger.new, so check out the documentation for the standard Logger class
@@ -33,12 +41,11 @@ module Mixlib
#
# It also configures the Logger instance it creates to use the custom Mixlib::Log::Formatter class.
def init(*opts)
- if opts.length == 0
- @logger = Logger.new(STDOUT)
- else
- @logger = Logger.new(*opts)
+ if @logger.nil?
+ @logger = (opts.empty? ? Logger.new(STDOUT) : Logger.new(*opts))
+ @logger.formatter = Mixlib::Log::Formatter.new()
end
- @logger.formatter = Mixlib::Log::Formatter.new()
+ @logger
end
# Sets the level for the Logger object by symbol. Valid arguments are:
@@ -51,34 +58,17 @@ module Mixlib
#
# Throws an ArgumentError if you feed it a bogus log level.
def level(loglevel=:warn)
- init() unless @logger
- case loglevel
- when :debug
- @logger.level = Logger::DEBUG
- when :info
- @logger.level = Logger::INFO
- when :warn
- @logger.level = Logger::WARN
- when :error
- @logger.level = Logger::ERROR
- when :fatal
- @logger.level = Logger::FATAL
- else
- raise ArgumentError, "Log level must be one of :debug, :info, :warn, :error, or :fatal"
- end
+ level = { :debug=>Logger::DEBUG, :info=>Logger::INFO, :warn=>Logger::WARN, :error=>Logger::ERROR, :fatal=>Logger::FATAL}[loglevel]
+ raise ArgumentError, "Log level must be one of :debug, :info, :warn, :error, or :fatal" if level.nil?
+ logger.level = level
end
# Passes any other method calls on directly to the underlying Logger object created with init. If
# this method gets hit before a call to Mixlib::Logger.init has been made, it will call
# Mixlib::Logger.init() with no arguments.
def method_missing(method_symbol, *args)
- init() unless @logger
- if args.length > 0
- @logger.send(method_symbol, *args)
- else
- @logger.send(method_symbol)
- end
+ logger.send(method_symbol, *args)
end
end
-end \ No newline at end of file
+end