diff options
-rw-r--r-- | lib/mixlib/log.rb | 14 | ||||
-rw-r--r-- | spec/mixlib/log_spec.rb | 24 | ||||
-rw-r--r-- | spec/spec_helper.rb | 23 |
3 files changed, 51 insertions, 10 deletions
diff --git a/lib/mixlib/log.rb b/lib/mixlib/log.rb index ba6dc92..a506ba2 100644 --- a/lib/mixlib/log.rb +++ b/lib/mixlib/log.rb @@ -63,15 +63,17 @@ module Mixlib # # Throws an ArgumentError if you feed it a bogus log level. def level=(l) - logger.level = @@levels[l] - raise ArgumentError, "Log level must be one of :debug, :info, :warn, :error, or :fatal" if logger.level.nil? + lv = @@levels[l] + raise ArgumentError, "Log level must be one of :debug, :info, :warn, :error, or :fatal" if lv.nil? + logger.level = lv end - def level - @@levels.each do |k,v| - return k if logger.level == v + def level(lv=nil) + if lv.nil? + @@levels.find() {|l| logger.level==l[1]}[0] + else + self.level=(lv) end - nil end # Passes any other method calls on directly to the underlying Logger object created with init. If diff --git a/spec/mixlib/log_spec.rb b/spec/mixlib/log_spec.rb index 259ae36..dcfa544 100644 --- a/spec/mixlib/log_spec.rb +++ b/spec/mixlib/log_spec.rb @@ -1,5 +1,6 @@ # # Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Christopher Brown (<cb@opscode.com>) # Copyright:: Copyright (c) 2008 Opscode, Inc. # License:: Apache License, Version 2.0 # @@ -34,7 +35,7 @@ describe Mixlib::Log do lambda { Logit.init(tf) }.should_not raise_error end - it "should set the log level with :debug, :info, :warn, :error, or :fatal" do + it "should set the log level using the binding form, with :debug, :info, :warn, :error, or :fatal" do levels = { :debug => Logger::DEBUG, :info => Logger::INFO, @@ -45,10 +46,29 @@ describe Mixlib::Log do levels.each do |symbol, constant| Logit.level(symbol) Logit.logger.level.should == constant + Logit.level.should == symbol end end - it "should raise an ArgumentError if you try and set the level to something strange" do + it "should set the log level using the method form, with :debug, :info, :warn, :error, or :fatal" do + levels = { + :debug => Logger::DEBUG, + :info => Logger::INFO, + :warn => Logger::WARN, + :error => Logger::ERROR, + :fatal => Logger::FATAL + } + levels.each do |symbol, constant| + Logit.level(symbol) + Logit.logger.level.should == constant + end + end + + it "should raise an ArgumentError if you try and set the level to something strange using the binding form" do + lambda { Logit.level = :the_roots }.should raise_error(ArgumentError) + end + + it "should raise an ArgumentError if you try and set the level to something strange using the method form" do lambda { Logit.level(:the_roots) }.should raise_error(ArgumentError) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 518498d..8640957 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,22 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Christopher Brown (<cb@opscode.com>) +# Copyright:: Copyright (c) 2008 Opscode, 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. +# + $TESTING=true $:.push File.join(File.dirname(__FILE__), '..', 'lib') @@ -5,5 +24,5 @@ require 'mixlib/log' require 'mixlib/log/formatter' class Logit - extend(Mixlib::Log) -end
\ No newline at end of file + extend Mixlib::Log +end |