Skip to content

Commit

Permalink
Reworked log capture in setting_with_deprecated_alias_spec to use Log…
Browse files Browse the repository at this point in the history
…ger's addAppender and a spy appender implementation
  • Loading branch information
andsel committed Nov 8, 2024
1 parent 6f65bf0 commit 2e99248
Showing 1 changed file with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -27,34 +28,40 @@
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, [])
@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
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")
java_import org.apache.logging.log4j.Level
logger.setLevel(Level::INFO)
expect(logger.info_enabled?).to be_truthy
# custom_appender = ::CustomAppender.new(events)
custom_appender = ::CustomAppender.send(:initialize, events)
logger.addAppender(custom_appender)

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)

settings.register(canonical_setting.with_deprecated_alias(deprecated_setting_name))
end

after(:each) do
@log_ctx.close
@log_spy = nil
@log_ctx = nil
end

shared_examples '#validate_value success' do
context '#validate_value' do
it "returns without raising" do
Expand All @@ -78,8 +85,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
Expand All @@ -97,15 +103,15 @@ def log_spy
end

it 'logs a deprecation warning' do
expect(log_spy.messages[0]).to include(deprecated_setting_name)
expect(events[0].message.formatted_message).to include(deprecated_setting_name)
end

include_examples '#validate_value success'

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].message.formatted_message).to include(deprecated_setting_name)
end
end

Expand Down Expand Up @@ -142,15 +148,15 @@ 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'

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
Expand All @@ -172,7 +178,7 @@ 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}`")
expect(events[0].message.formatted_message).to include("setting `#{canonical_setting_name}` has been queried by its deprecated alias `#{deprecated_setting_name}`")
end
end
end

0 comments on commit 2e99248

Please sign in to comment.