diff --git a/logstash-core/spec/logstash/settings/setting_with_deprecated_alias_spec.rb b/logstash-core/spec/logstash/settings/setting_with_deprecated_alias_spec.rb index 168162ee088..d340b261973 100644 --- a/logstash-core/spec/logstash/settings/setting_with_deprecated_alias_spec.rb +++ b/logstash-core/spec/logstash/settings/setting_with_deprecated_alias_spec.rb @@ -17,6 +17,7 @@ require "spec_helper" require "logstash/settings" +java_import org.apache.logging.log4j.core.appender.AbstractAppender describe LogStash::Setting::SettingWithDeprecatedAlias do let(:canonical_setting_name) { "canonical.setting" } @@ -27,21 +28,38 @@ let(:settings) { LogStash::Settings.new } let(:canonical_setting) { LogStash::Setting::StringSetting.new(canonical_setting_name, default_value, true) } - log_spy = nil - log_ctx = nil + class CustomAppender < AbstractAppender - def log_ctx - @log_ctx - end + attr_reader :events_collector + + def initialize(events) + super("CustomCaptorAppender", nil, nil, true, org.apache.logging.log4j.core.config.Property::EMPTY_ARRAY) + @events_collector = events + end - def log_spy - @log_spy + # override the append to catch all the calls and collect the events + def append(log_event) + @events_collector << log_event.message.formatted_message + end end + let(:events) { [] } + before(:each) do - # Initialization of appender and logger use to spy, need to be freshly recreated on each test is context shutdown is used. - @log_ctx = setup_logger_spy - @log_spy = retrieve_logger_spy(@log_ctx) + java_import org.apache.logging.log4j.LogManager + logger = LogManager.getLogger("org.logstash.settings.DeprecatedAlias") + deprecated_logger = LogManager.getLogger("org.logstash.deprecation.settings.DeprecatedAlias") + + @custom_appender = CustomAppender.new(events).tap {|appender| appender.start } + + java_import org.apache.logging.log4j.Level + logger.addAppender(@custom_appender) + deprecated_logger.addAppender(@custom_appender) + # had to set level after appending as it was "error" for some reason + logger.setLevel(Level::INFO) + deprecated_logger.setLevel(Level::INFO) + + expect(@custom_appender.started?).to be_truthy allow(LogStash::Settings).to receive(:logger).and_return(double("SettingsLogger").as_null_object) allow(LogStash::Settings).to receive(:deprecation_logger).and_return(double("SettingsDeprecationLogger").as_null_object) @@ -50,9 +68,15 @@ def log_spy end after(:each) do - @log_ctx.close - @log_spy = nil - @log_ctx = nil + events.clear + java_import org.apache.logging.log4j.LogManager + logger = LogManager.getLogger("org.logstash.settings.DeprecatedAlias") + deprecated_logger = LogManager.getLogger("org.logstash.deprecation.settings.DeprecatedAlias") + # The Logger's AbstractConfiguration contains a cache of Appender, by class name. The cache is updated + # iff is absent, so to make subsequent add_appender effective we have cleanup on teardown, else the new + # appender instance is simply not used by the logger + logger.remove_appender(@custom_appender) + deprecated_logger.remove_appender(@custom_appender) end shared_examples '#validate_value success' do @@ -78,8 +102,7 @@ def log_spy it 'does not emit a deprecation warning' do expect(LogStash::Settings.deprecation_logger).to_not receive(:deprecated).with(a_string_including(deprecated_setting_name)) settings.get_setting(deprecated_setting_name).observe_post_process - log_spy = retrieve_logger_spy(log_ctx) - expect(log_spy.messages).to be_empty + expect(events).to be_empty end end end @@ -97,7 +120,7 @@ def log_spy end it 'logs a deprecation warning' do - expect(log_spy.messages[0]).to include(deprecated_setting_name) + expect(events[0]).to include(deprecated_setting_name) end include_examples '#validate_value success' @@ -105,7 +128,7 @@ def log_spy context "#observe_post_process" do it 're-emits the deprecation warning' do settings.get_setting(deprecated_setting_name).observe_post_process - expect(log_spy.messages[0]).to include(deprecated_setting_name) + expect(events[0]).to include(deprecated_setting_name) end end @@ -142,7 +165,7 @@ def log_spy it 'does not produce a relevant deprecation warning' do settings.get_setting(deprecated_setting_name).observe_post_process - expect(log_spy.messages).to be_empty + expect(events).to be_empty end include_examples '#validate_value success' @@ -150,7 +173,7 @@ def log_spy context "#observe_post_process" do it 'does not emit a deprecation warning' do settings.get_setting(deprecated_setting_name).observe_post_process - expect(log_spy.messages).to be_empty + expect(events).to be_empty end end end @@ -172,7 +195,8 @@ def log_spy context 'Settings#get on deprecated alias' do it 'produces a WARN-level message to the logger' do settings.get(deprecated_setting_name) - expect(log_spy.messages[0]).to include("setting `#{canonical_setting_name}` has been queried by its deprecated alias `#{deprecated_setting_name}`") + sleep 0.1 + expect(events[0]).to include("setting `#{canonical_setting_name}` has been queried by its deprecated alias `#{deprecated_setting_name}`") end end end