Skip to content

Commit 0602b98

Browse files
committed
Moved log appender spy class into spec_helper, used the same tecnhique also in runner_spec, removing the programmatic configuration way
1 parent f5380e7 commit 0602b98

File tree

3 files changed

+43
-86
lines changed

3 files changed

+43
-86
lines changed

logstash-core/spec/logstash/runner_spec.rb

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -263,41 +263,40 @@
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-
def configure_log_spy
267-
java_import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
268-
java_import org.apache.logging.log4j.Level
269-
config_builder = ConfigurationBuilderFactory.newConfigurationBuilder
270-
return config_builder
271-
.add(
272-
config_builder
273-
.newAppender("LOG_SPY", "List")
274-
.add(config_builder.newLayout("PatternLayout").addAttribute("pattern", "%-5p [%t]: %m%n"))
275-
)
276-
.add(
277-
config_builder
278-
.newRootLogger(Level::INFO)
279-
.add(config_builder.newAppenderRef("LOG_SPY")))
280-
.build(false)
281-
end
282-
283266
context "when deprecated :http.host is defined by the user" do
284267
let(:args) { ["--http.host", "localhost", "-e", pipeline_string]}
285-
it "creates an Agent whose `api.http.host` uses the provided value and provides helpful deprecation message" do
286-
java_import org.apache.logging.log4j.core.config.Configurator
287-
java_import org.apache.logging.log4j.core.config.Configuration
268+
let(:events) { [] }
288269

270+
before(:each) do
289271
java_import org.apache.logging.log4j.LogManager
290-
java_import org.apache.logging.log4j.core.impl.Log4jContextFactory
291-
# This is done because the LoggerExt calls setFactory LogstashLoggerContextFactory implements
292-
# org.apache.logging.log4j.spi.LoggerContextFactory and doesn't extend org.apache.logging.log4j.core.impl.Log4jContextFactory
293-
# which is the class expected by the following Configurator to use the programmatic configuration.
294-
LogManager.setFactory(Log4jContextFactory.new)
295-
log_ctx = Configurator.java_send(:initialize, [Configuration], configure_log_spy)
296-
expect(log_ctx).not_to be nil
297-
log_ctx.reconfigure(configure_log_spy) # force the programmatic configuration, without this it's not used
272+
logger = LogManager.getLogger("org.logstash.settings.DeprecatedAlias")
273+
deprecated_logger = LogManager.getLogger("org.logstash.deprecation.settings.DeprecatedAlias")
298274

299-
appender_spy = log_ctx.getConfiguration().getAppender("LOG_SPY")
275+
@custom_appender = CustomAppender.new(events).tap {|appender| appender.start }
276+
277+
java_import org.apache.logging.log4j.Level
278+
logger.addAppender(@custom_appender)
279+
deprecated_logger.addAppender(@custom_appender)
280+
# had to set level after appending as it was "error" for some reason
281+
logger.setLevel(Level::INFO)
282+
deprecated_logger.setLevel(Level::INFO)
283+
284+
expect(@custom_appender.started?).to be_truthy
285+
end
300286

287+
after(:each) do
288+
events.clear
289+
java_import org.apache.logging.log4j.LogManager
290+
logger = LogManager.getLogger("org.logstash.settings.DeprecatedAlias")
291+
deprecated_logger = LogManager.getLogger("org.logstash.deprecation.settings.DeprecatedAlias")
292+
# The Logger's AbstractConfiguration contains a cache of Appender, by class name. The cache is updated
293+
# iff is absent, so to make subsequent add_appender effective we have cleanup on teardown, else the new
294+
# appender instance is simply not used by the logger
295+
logger.remove_appender(@custom_appender)
296+
deprecated_logger.remove_appender(@custom_appender)
297+
end
298+
299+
it "creates an Agent whose `api.http.host` uses the provided value and provides helpful deprecation message" do
301300
expect(runner_deprecation_logger_stub).to receive(:deprecated).with(a_string_including 'The flag ["--http.host"] has been deprecated')
302301
expect(LogStash::Agent).to receive(:new) do |settings|
303302
expect(settings.set?("api.http.host")).to be(true)
@@ -306,10 +305,8 @@ def configure_log_spy
306305

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

309-
expect(appender_spy.messages).not_to be_empty
310-
expect(appender_spy.messages[0]).to match(/`http.host` is a deprecated alias for `api.http.host`/)
311-
312-
log_ctx.close
308+
expect(events).not_to be_empty
309+
expect(events[0]).to match(/`http.host` is a deprecated alias for `api.http.host`/)
313310
end
314311
end
315312

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
require "spec_helper"
1919
require "logstash/settings"
20-
java_import org.apache.logging.log4j.core.appender.AbstractAppender
2120

2221
describe LogStash::Setting::SettingWithDeprecatedAlias do
2322
let(:canonical_setting_name) { "canonical.setting" }
@@ -28,21 +27,6 @@
2827
let(:settings) { LogStash::Settings.new }
2928
let(:canonical_setting) { LogStash::Setting::StringSetting.new(canonical_setting_name, default_value, true) }
3029

31-
class CustomAppender < AbstractAppender
32-
33-
attr_reader :events_collector
34-
35-
def initialize(events)
36-
super("CustomCaptorAppender", nil, nil, true, org.apache.logging.log4j.core.config.Property::EMPTY_ARRAY)
37-
@events_collector = events
38-
end
39-
40-
# override the append to catch all the calls and collect the events
41-
def append(log_event)
42-
@events_collector << log_event.message.formatted_message
43-
end
44-
end
45-
4630
let(:events) { [] }
4731

4832
before(:each) do
@@ -195,7 +179,6 @@ def append(log_event)
195179
context 'Settings#get on deprecated alias' do
196180
it 'produces a WARN-level message to the logger' do
197181
settings.get(deprecated_setting_name)
198-
sleep 0.1
199182
expect(events[0]).to include("setting `#{canonical_setting_name}` has been queried by its deprecated alias `#{deprecated_setting_name}`")
200183
end
201184
end

spec/spec_helper.rb

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -91,42 +91,19 @@ def installed_plugins
9191
Gem::Specification.find_all.select { |spec| spec.metadata["logstash_plugin"] }.map { |plugin| plugin.name }
9292
end
9393

94+
java_import org.apache.logging.log4j.core.appender.AbstractAppender
9495

95-
def setup_logger_spy
96-
java_import org.apache.logging.log4j.core.config.Configurator
97-
java_import org.apache.logging.log4j.core.config.Configuration
98-
99-
java_import org.apache.logging.log4j.LogManager
100-
java_import org.apache.logging.log4j.core.impl.Log4jContextFactory
101-
# This is done because the LoggerExt calls setFactory LogstashLoggerContextFactory implements
102-
# org.apache.logging.log4j.spi.LoggerContextFactory and doesn't extend org.apache.logging.log4j.core.impl.Log4jContextFactory
103-
# which is the class expected by the following Configurator to use the programmatic configuration.
104-
LogManager.setFactory(Log4jContextFactory.new)
105-
log_ctx = Configurator.java_send(:initialize, [Configuration], configure_log_spy)
106-
expect(log_ctx).not_to be nil
107-
log_ctx.reconfigure(configure_log_spy) # force the programmatic configuration, without this it's not used
108-
109-
log_ctx
110-
end
96+
class CustomAppender < AbstractAppender
11197

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
128-
end
98+
attr_reader :events_collector
12999

130-
def retrieve_logger_spy(log_ctx)
131-
log_ctx.getConfiguration().getAppender("LOG_SPY")
132-
end
100+
def initialize(events)
101+
super("CustomCaptorAppender", nil, nil, true, org.apache.logging.log4j.core.config.Property::EMPTY_ARRAY)
102+
@events_collector = events
103+
end
104+
105+
# override the append to catch all the calls and collect the events
106+
def append(log_event)
107+
@events_collector << log_event.message.formatted_message
108+
end
109+
end

0 commit comments

Comments
 (0)