8 @logger = mock("logger provider")
10 @logger.stubs(:set_logging_level)
11 @logger.stubs(:valid_levels)
12 @logger.stubs(:should_log?).returns(true)
13 @logger.stubs(:new).returns(@logger)
15 # we stub it out at the top of the test suite
17 Log.unstub(:logexception)
21 Log.set_logger(@logger)
24 describe "#config_and_check_level" do
25 it "should configure then not already configured" do
26 Log.expects(:configure)
27 Log.config_and_check_level(:debug)
30 it "should not reconfigure the logger" do
31 Log.configure(@logger)
32 Log.expects(:configure).never
33 Log.config_and_check_level(:debug)
36 it "should check the level is valid" do
37 Log.configure(@logger)
38 Log.expects(:check_level).with(:debug)
39 Log.config_and_check_level(:debug)
42 it "should respect the loggers decision about levels" do
43 Log.configure(@logger)
44 @logger.expects(:should_log?).returns(false)
45 Log.config_and_check_level(:debug).should == false
49 describe "#valid_level?" do
50 it "should correctly report for valid levels" do
51 [:error, :fatal, :debug, :warn, :info].each {|level| Log.valid_level?(level).should == true }
52 Log.valid_level?(:rspec).should == false
56 describe "#message_for" do
57 it "should return the code and retrieved message" do
58 Util.expects(:t).with(:PLMC1, {:rspec => true}).returns("this is PLMC1")
59 Log.message_for(:PLMC1, {:rspec => true}).should == "PLMC1: this is PLMC1"
63 describe "#logexception" do
64 it "should short circuit messages below current level" do
65 Log.expects(:config_and_check_level).with(:debug).returns(false)
66 Log.expects(:log).never
67 Log.logexception(:PLMC1, :debug, Exception.new, {})
70 it "should request the message including the exception string and log it" do
71 Log.stubs(:config_and_check_level).returns(true)
72 Log.expects(:message_for).with(:PLMC1, {:rspec => "test", :error => "Exception: this is a test"}).returns("This is a test")
73 Log.expects(:log).with(:debug, "This is a test", "test:2")
75 e = Exception.new("this is a test")
76 e.set_backtrace ["/some/dir/test:1", "/some/dir/test:2"]
78 Log.logexception(:PLMC1, :debug, e, false, {:rspec => "test"})
83 it "should short circuit messages below current level" do
84 Log.expects(:config_and_check_level).with(:debug).returns(false)
85 Log.expects(:log).never
86 Log.logmsg(:PLMC1, "", :debug, {})
89 it "should request the message and log it" do
90 Log.stubs(:config_and_check_level).returns(true)
91 Log.expects(:message_for).with(:PLMC1, {:rspec => "test", :default => "default"}).returns("This is a test")
92 Log.expects(:log).with(:debug, "This is a test")
93 Log.logmsg(:PLMC1, "default", :debug, :rspec => "test")
97 describe "#check_level" do
98 it "should check for valid levels" do
99 Log.expects(:valid_level?).with(:debug).returns(true)
100 Log.check_level(:debug)
103 it "should raise for invalid levels" do
104 expect { Log.check_level(:rspec) }.to raise_error("Unknown log level")
108 describe "#configure" do
109 it "should default to console logging if called prior to configuration" do
110 Config.instance.instance_variable_set("@configured", false)
112 Log.logger.should == MCollective::Logger::Console_logger
116 describe "#instance" do
117 it "should return the correct reference" do
118 Log.configure(@logger)
119 Log.instance.should == MCollective::Log
124 it "should log at the right levels" do
125 Log.configure(@logger)
127 [:debug, :info, :fatal, :error, :warn].each do |level|
128 @logger.expects(:log).with(level, anything, regexp_matches(/#{level} test/))
129 @logger.expects(:should_log?).with(level).returns(true)
130 Log.send(level, "#{level} test")
135 describe "#cycle_level" do
136 it "should cycle logger class levels" do
137 @logger.expects(:cycle_level)
139 Log.configure(@logger)