test/appender/splunk_test.rb in semantic_logger-3.2.1 vs test/appender/splunk_test.rb in semantic_logger-3.3.0

- old
+ new

@@ -15,67 +15,70 @@ end describe SemanticLogger::Appender::Splunk do before do SemanticLogger::Appender::Splunk.stub_any_instance(:reopen, nil) do - @appender = SemanticLogger::Appender::Splunk.new(level: :info) + @appender = SemanticLogger::Appender::Splunk.new end @message = 'AppenderSplunkTest log message' end - it 'not send :trace notifications to Splunk when set to :error' do - mock = Mock.new - @appender.stub(:service_index, mock) do - @appender.trace('AppenderSplunkTest trace message') + SemanticLogger::LEVELS.each do |level| + it "send #{level}" do + mock = Mock.new + @appender.stub(:service_index, mock) do + @appender.send(level, @message) + end + assert_equal @message, mock.message + assert_equal level, mock.event[:event][:level] + refute mock.event[:event][:exception] end - assert_nil mock.event - assert_nil mock.message - end - it 'send exception notifications to Splunk with severity' do - hash = nil - exc = nil - begin - Uh oh - rescue Exception => e - exc = e + it "sends #{level} exceptions" do + exc = nil + begin + Uh oh + rescue Exception => e + exc = e + end + + mock = Mock.new + @appender.stub(:service_index, mock) do + @appender.send(level, @message, exc) + end + assert_equal @message, mock.message + + assert exception = mock.event[:event][:exception] + assert 'NameError', exception[:name] + assert 'undefined local variable or method', exception[:message] + assert_equal level, mock.event[:event][:level] + assert exception[:stack_trace].first.include?(__FILE__), exception end - mock = Mock.new - @appender.stub(:service_index, mock) do - @appender.error 'Reading File', exc - end - assert_equal 'Reading File', mock.message - hash = mock.event - refute hash[:message] - assert 'NameError', hash[:exception][:name] - assert 'undefined local variable or method', hash[:exception][:message] - assert_equal 4, hash[:level_index], 'Should be error level (4)' - assert_equal :error, hash[:level] - assert hash[:exception][:stack_trace].first.include?(__FILE__), hash[:exception] - end - it 'send error notifications to Splunk with severity' do - mock = Mock.new - @appender.stub(:service_index, mock) do - @appender.error @message + it "sends #{level} custom attributes" do + mock = Mock.new + @appender.stub(:service_index, mock) do + @appender.send(level, @message, {key1: 1, key2: 'a'}) + end + assert_equal @message, mock.message + + assert mock.event[:event], mock.event.ai + assert_equal level, mock.event[:event][:level] + refute mock.event[:event][:stack_trace] + assert_equal(1, mock.event[:event][:key1], mock.event) + assert_equal('a', mock.event[:event][:key2], mock.event) end - assert_equal @message, mock.message - assert_equal :error, mock.event[:level] - refute mock.event[:stack_trace] end - it 'send notification to Splunk with custom attributes' do - mock = Mock.new + it 'does not send :trace notifications to Splunk when set to :error' do + mock = Mock.new + @appender.level = :error @appender.stub(:service_index, mock) do - @appender.error @message, {key1: 1, key2: 'a'} + @appender.trace('AppenderSplunkTest trace message') end - assert_equal @message, mock.message - hash = mock.event - assert_equal :error, hash[:level] - refute hash[:stack_trace] - assert_equal(1, hash[:key1], hash) - assert_equal('a', hash[:key2], hash) + assert_nil mock.event + assert_nil mock.message end - end + end end