spec/lib/logstasher_spec.rb in logstasher-0.4.9 vs spec/lib/logstasher_spec.rb in logstasher-0.5.0

- old
+ new

@@ -64,31 +64,46 @@ ActionController::Base.should_receive(:send).with(:define_method, :logtasher_add_custom_fields_to_payload, &block) LogStasher.add_custom_fields(&block) end end - describe '.setup' do + shared_examples 'setup' do let(:logger) { double } - let(:logstasher_config) { double(:logger => logger,:log_level => 'warn',:log_controller_parameters => nil) } + let(:logstasher_config) { double(:logger => logger, :log_level => 'warn', :log_controller_parameters => nil, :source => logstasher_source) } let(:config) { double(:logstasher => logstasher_config) } let(:app) { double(:config => config) } before do + @previous_source = LogStasher.source config.stub(:action_dispatch => double(:rack_cache => false)) end + after { LogStasher.source = @previous_source } # Need to restore old source for specs it 'defines a method in ActionController::Base' do LogStasher.should_receive(:require).with('logstasher/rails_ext/action_controller/metal/instrumentation') LogStasher.should_receive(:require).with('logstash-event') LogStasher.should_receive(:suppress_app_logs).with(app) LogStasher::RequestLogSubscriber.should_receive(:attach_to).with(:action_controller) logger.should_receive(:level=).with('warn') LogStasher.setup(app) + LogStasher.source.should == (logstasher_source || 'unknown') LogStasher.enabled.should be_true LogStasher.custom_fields.should == [] LogStasher.log_controller_parameters.should == false end end + describe '.setup' do + describe 'with source set' do + let(:logstasher_source) { 'foo' } + it_behaves_like 'setup' + end + + describe 'without source set (default behaviour)' do + let(:logstasher_source) { nil } + it_behaves_like 'setup' + end + end + describe '.suppress_app_logs' do let(:logstasher_config){ double(:logstasher => double(:suppress_app_log => true))} let(:app){ double(:config => logstasher_config)} it 'removes existing subscription if enabled' do LogStasher.should_receive(:require).with('logstasher/rails_ext/rack/logger') @@ -137,9 +152,19 @@ end it 'adds to log with specified level' do logger.should_receive(:send).with('warn?').and_return(true) logger.should_receive(:send).with('warn',"{\"@source\":\"unknown\",\"@tags\":[\"log\"],\"@fields\":{\"message\":\"WARNING\",\"level\":\"warn\"},\"@timestamp\":\"timestamp\"}") LogStasher.log('warn', 'WARNING') + end + context 'with a source specified' do + before :each do + LogStasher.source = 'foo' + end + it 'sets the correct source' do + logger.should_receive(:send).with('warn?').and_return(true) + logger.should_receive(:send).with('warn',"{\"@source\":\"foo\",\"@tags\":[\"log\"],\"@fields\":{\"message\":\"WARNING\",\"level\":\"warn\"},\"@timestamp\":\"timestamp\"}") + LogStasher.log('warn', 'WARNING') + end end end %w( fatal error warn info debug unknown ).each do |severity| describe ".#{severity}" do