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
0 commit comments