Permalink
Browse files

Merge branch 'release/forwardable'

  • Loading branch information...
2 parents 0482bb1 + 32f7e4b commit 2ada1512a307d4e7bbcbc08ead046c3940c1f8eb @jjuarez committed Jul 7, 2011
Showing with 53 additions and 64 deletions.
  1. +37 −56 lib/mini_logger.rb
  2. +2 −2 lib/version.rb
  3. +14 −6 test/test_mini_logger.rb
View
@@ -1,10 +1,12 @@
-require 'rubygems'
require 'logger'
require 'yaml'
+require 'forwardable'
-module MiniLogger
- extend self
+class MiniLogger
+ extend Forwardable
+
+ def_delegators :@logger, :debug, :info, :warn, :error, :fatal, :debug?, :info?, :warn?, :error?, :fatal?
DEBUG = :debug
INFO = :info
@@ -28,93 +30,72 @@ module MiniLogger
::Logger::FATAL => FATAL
}
- @logger = nil
-
- def self.standarize_log_level(ll)
+ private
+ def self.standarize_log_level(level)
- case ll
- when String then LLM[ll.downcase.to_sym]
- when Symbol then LLM[ll.to_s.downcase.to_sym]
+ case level
+ when String then LLM[level.downcase.to_sym]
+ when Symbol then LLM[level.to_s.downcase.to_sym]
+ else ::Logger::INFO
end
end
- def self.validate_log_level?(ll)
+ public
+ def self.validate_log_level?(level)
- case ll
- when String then LLM.has_key?(ll.downcase.to_sym)
- when Symbol then LLM.has_key?(ll.to_s.downcase.to_sym)
+ case level
+ when String then LLM.has_key?(level.downcase.to_sym)
+ when Symbol then LLM.has_key?(level.to_s.downcase.to_sym)
else false
end
end
- def configure(*arguments)
+ def self.configure(*arguments)
- configuration = {}
+ configuration = Hash.new
case arguments[0]
- when String
- configuration.merge!(YAML.load_file(arguments[0]))
- when Hash
- configuration.merge!(arguments[0])
- else
- configuration = { :dev=>STDERR, :level=>:debug }
+ when String then configuration.merge!(YAML.load_file(arguments[0]))
+ when Hash then configuration.merge!(arguments[0])
+ else configuration = { :dev=>STDERR, :level=>:debug }
end
configuration = { :log_channel=>STDERR, :log_level=>:info }.merge(configuration)
- configuration[:dev] = configuration[:log_channel] unless configuration[:dev]
- configuration[:level] = configuration[:log_level] unless configuration[:level]
+ configuration[:dev] ||= configuration[:log_channel]
+ configuration[:level] ||= configuration[:log_level]
raise ArgumentError.new("Invalid log level") unless validate_log_level?(configuration[:level])
- configuration[:dev] = case configuration[:dev]
- when /(STDOUT|stdout)/ then STDOUT
- when /(STDERR|stderr)/ then STDERR
- when :stdout then STDOUT
- when :stderr then STDERR
+ configuration[:dev] = case configuration[:dev].to_s.downcase
+ when /stdout/ then STDOUT
+ when /stderr/ then STDERR
else configuration[:dev]
end
- @logger = ::Logger.new(configuration[:dev])
- @logger.level = standarize_log_level(configuration[:level])
-
- self
+ self.new(configuration)
end
- def level=(nll)
+ def initialize(options)
- if @logger
+ @logger = ::Logger.new(options[:dev])
+ @logger.level = MiniLogger.standarize_log_level(options[:level])
- raise ArgumentError.new("Invalid log level #{nll.class.name}:'#{nll}'") unless validate_log_level?(nll)
- @logger.level = standarize_log_level(nll)
-
- self
- end
+ self
end
- def level!(nll)
+ def level=(level)
- if @logger
-
- raise ArgumentError.new("Invalid log level #{nll.class.name}:'#{nll}'") unless validate_log_level?(nll)
- @logger.level = standarize_log_level(nll)
-
- self
- end
+ raise ArgumentError.new("Invalid log level #{level.class.name}:'#{level}'") unless MiniLogger.validate_log_level?(level)
+ @logger.level = MiniLogger.standarize_log_level(level)
+
+ self
end
def level
- RLLM[@logger.level] if @logger
+ RLLM[@logger.level]
end
-
- def method_missing(method, *arguments, &block)
-
- if @logger && [:debug, :info, :warn, :error, :fatal, :debug?, :info?, :warn?, :error?, :fatal?].include?(method)
-
- @logger.send(method, *arguments, &block)
- end
- end
end
View
@@ -3,8 +3,8 @@ class Version
INFO = {
:major =>0,
- :minor =>5,
- :patch =>3
+ :minor =>6,
+ :patch =>0
}
def self.number(version_info=INFO)
View
@@ -122,13 +122,21 @@ class TestMiniLogger < Test::Unit::TestCase
test_logger.level = MiniLogger::ERROR
assert test_logger.error?
- assert_equal(test_logger.level, MiniLogger::ERROR)
- assert test_logger.level!(MiniLogger::DEBUG).debug?
- assert test_logger.level!(MiniLogger::INFO).info?
- assert test_logger.level!(MiniLogger::WARN).warn?
- assert test_logger.level!(MiniLogger::ERROR).error?
- assert test_logger.level!(MiniLogger::FATAL).fatal?
+ test_logger.level=MiniLogger::DEBUG
+ assert test_logger.debug?
+
+ test_logger.level=MiniLogger::INFO
+ assert test_logger.info?
+
+ test_logger.level=MiniLogger::WARN
+ assert test_logger.warn?
+
+ test_logger.level=MiniLogger::ERROR
+ assert test_logger.error?
+
+ test_logger.level=MiniLogger::FATAL
+ assert test_logger.fatal?
end

0 comments on commit 2ada151

Please sign in to comment.