Skip to content

Commit 6f65bf0

Browse files
committed
Force to use freach Log4j configuration on each test, in particaular when it closes the logger context
1 parent 8ffcede commit 6f65bf0

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

logstash-core/spec/logstash/runner_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,6 @@
263263
let(:runner_deprecation_logger_stub) { double("DeprecationLogger(Runner)").as_null_object }
264264
before(:each) { allow(runner).to receive(:deprecation_logger).and_return(runner_deprecation_logger_stub) }
265265

266-
let(:configuration_spy) { configure_log_spy }
267-
268266
def configure_log_spy
269267
java_import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
270268
java_import org.apache.logging.log4j.Level
@@ -308,7 +306,10 @@ def configure_log_spy
308306

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

309+
expect(appender_spy.messages).not_to be_empty
311310
expect(appender_spy.messages[0]).to match(/`http.host` is a deprecated alias for `api.http.host`/)
311+
312+
log_ctx.close
312313
end
313314
end
314315

logstash-core/spec/logstash/settings/setting_with_deprecated_alias_spec.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,34 @@
2727
let(:settings) { LogStash::Settings.new }
2828
let(:canonical_setting) { LogStash::Setting::StringSetting.new(canonical_setting_name, default_value, true) }
2929

30-
let(:log_ctx) { setup_logger_spy }
31-
let(:log_spy) { retrieve_logger_spy(log_ctx) }
30+
log_spy = nil
31+
log_ctx = nil
3232

33-
before(:each) do
34-
# Initialization of appender and logger use to spy, need to be done before executing any code that logs,
35-
# that's the reason wy to refer the spying logger context before any test.
36-
log_ctx
33+
def log_ctx
34+
@log_ctx
35+
end
36+
37+
def log_spy
38+
@log_spy
3739
end
3840

3941
before(:each) do
42+
# Initialization of appender and logger use to spy, need to be freshly recreated on each test is context shutdown is used.
43+
@log_ctx = setup_logger_spy
44+
@log_spy = retrieve_logger_spy(@log_ctx)
45+
4046
allow(LogStash::Settings).to receive(:logger).and_return(double("SettingsLogger").as_null_object)
4147
allow(LogStash::Settings).to receive(:deprecation_logger).and_return(double("SettingsDeprecationLogger").as_null_object)
4248

4349
settings.register(canonical_setting.with_deprecated_alias(deprecated_setting_name))
4450
end
4551

52+
after(:each) do
53+
@log_ctx.close
54+
@log_spy = nil
55+
@log_ctx = nil
56+
end
57+
4658
shared_examples '#validate_value success' do
4759
context '#validate_value' do
4860
it "returns without raising" do

spec/spec_helper.rb

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,6 @@ def installed_plugins
9393

9494

9595
def setup_logger_spy
96-
java_import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
97-
java_import org.apache.logging.log4j.Level
98-
config_builder = ConfigurationBuilderFactory.newConfigurationBuilder
99-
configure_log_spy = config_builder
100-
.add(
101-
config_builder
102-
.newAppender("LOG_SPY", "List")
103-
.add(config_builder.newLayout("PatternLayout").addAttribute("pattern", "%-5p [%t]: %m%n"))
104-
)
105-
.add(
106-
config_builder
107-
.newRootLogger(Level::INFO)
108-
.add(config_builder.newAppenderRef("LOG_SPY")))
109-
.build(false)
110-
11196
java_import org.apache.logging.log4j.core.config.Configurator
11297
java_import org.apache.logging.log4j.core.config.Configuration
11398

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

124-
return log_ctx
109+
log_ctx
110+
end
111+
112+
def configure_log_spy
113+
java_import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
114+
java_import org.apache.logging.log4j.Level
115+
config_builder = ConfigurationBuilderFactory.newConfigurationBuilder
116+
configuration = config_builder
117+
.add(
118+
config_builder
119+
.newAppender("LOG_SPY", "List")
120+
.add(config_builder.newLayout("PatternLayout").addAttribute("pattern", "%-5p [%t]: %m%n"))
121+
)
122+
.add(
123+
config_builder
124+
.newRootLogger(Level::INFO)
125+
.add(config_builder.newAppenderRef("LOG_SPY")))
126+
.build(false)
127+
configuration
125128
end
126129

127130
def retrieve_logger_spy(log_ctx)

0 commit comments

Comments
 (0)