Skip to content

Commit

Permalink
Switched capture strategy directly adding spy appender (#3)
Browse files Browse the repository at this point in the history
Reworked log capture in setting_with_deprecated_alias_spec to use Logger's addAppender and a spy appender implementation.
This leverages the idea to don't reconfigure the Log4J with a custom build configuration, but simply adding an appender that could spy the appended lines to a logger.
  • Loading branch information
andsel authored Nov 12, 2024
1 parent 6f65bf0 commit f5380e7
Showing 1 changed file with 44 additions and 20 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,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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -97,15 +120,15 @@ 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'

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

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

0 comments on commit f5380e7

Please sign in to comment.