Skip to content

Commit

Permalink
Force to use freach Log4j configuration on each test, in particaular …
Browse files Browse the repository at this point in the history
…when it closes the logger context
  • Loading branch information
andsel committed Nov 5, 2024
1 parent 8ffcede commit 6f65bf0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
5 changes: 3 additions & 2 deletions logstash-core/spec/logstash/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,6 @@
let(:runner_deprecation_logger_stub) { double("DeprecationLogger(Runner)").as_null_object }
before(:each) { allow(runner).to receive(:deprecation_logger).and_return(runner_deprecation_logger_stub) }

let(:configuration_spy) { configure_log_spy }

def configure_log_spy
java_import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
java_import org.apache.logging.log4j.Level
Expand Down Expand Up @@ -308,7 +306,10 @@ def configure_log_spy

subject.run("bin/logstash", args)

expect(appender_spy.messages).not_to be_empty
expect(appender_spy.messages[0]).to match(/`http.host` is a deprecated alias for `api.http.host`/)

log_ctx.close
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,34 @@
let(:settings) { LogStash::Settings.new }
let(:canonical_setting) { LogStash::Setting::StringSetting.new(canonical_setting_name, default_value, true) }

let(:log_ctx) { setup_logger_spy }
let(:log_spy) { retrieve_logger_spy(log_ctx) }
log_spy = nil
log_ctx = nil

before(:each) do
# Initialization of appender and logger use to spy, need to be done before executing any code that logs,
# that's the reason wy to refer the spying logger context before any test.
log_ctx
def log_ctx
@log_ctx
end

def log_spy
@log_spy
end

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)

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 Down
35 changes: 19 additions & 16 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,6 @@ def installed_plugins


def setup_logger_spy
java_import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
java_import org.apache.logging.log4j.Level
config_builder = ConfigurationBuilderFactory.newConfigurationBuilder
configure_log_spy = config_builder
.add(
config_builder
.newAppender("LOG_SPY", "List")
.add(config_builder.newLayout("PatternLayout").addAttribute("pattern", "%-5p [%t]: %m%n"))
)
.add(
config_builder
.newRootLogger(Level::INFO)
.add(config_builder.newAppenderRef("LOG_SPY")))
.build(false)

java_import org.apache.logging.log4j.core.config.Configurator
java_import org.apache.logging.log4j.core.config.Configuration

Expand All @@ -121,7 +106,25 @@ def setup_logger_spy
expect(log_ctx).not_to be nil
log_ctx.reconfigure(configure_log_spy) # force the programmatic configuration, without this it's not used

return log_ctx
log_ctx
end

def configure_log_spy
java_import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
java_import org.apache.logging.log4j.Level
config_builder = ConfigurationBuilderFactory.newConfigurationBuilder
configuration = config_builder
.add(
config_builder
.newAppender("LOG_SPY", "List")
.add(config_builder.newLayout("PatternLayout").addAttribute("pattern", "%-5p [%t]: %m%n"))
)
.add(
config_builder
.newRootLogger(Level::INFO)
.add(config_builder.newAppenderRef("LOG_SPY")))
.build(false)
configuration
end

def retrieve_logger_spy(log_ctx)
Expand Down

0 comments on commit 6f65bf0

Please sign in to comment.