LogstashCustomizer implements Customizer { @Override public void customize() { if (logstash.enabled()) { Properties handlerProps = new Properties(); handlerProps.put("hostname", this.logstash.hostname()); handlerProps.put("port", "" + this.logstash.port()); final CustomHandler<?> logstashHandler = new CustomHandler<>("logstash-handler") .module("org.jboss.logmanager.ext") .attributeClass("org.jboss.logmanager.ext.handlers.SocketHandler") .namedFormatter("logstash") .properties(handlerProps) .level(this.logstash.level()); this.logging .customFormatter("logstash", "org.jboss.logmanager.ext", "org.jboss.logmanager.ext.formatters.LogstashFormatter", this.logstash.formatterProperties()) .customHandler(logstashHandler) .subresources() .rootLogger() .handler(logstashHandler.getKey()); } }  @Override void customize();  }
@Test public void testNotEnabled() { this.customizer.customize(); assertThat( this.customizer.logging.subresources().customHandler("logstash-handler" ) ).isNull(); assertThat( this.customizer.logging.subresources().customFormatter( "logstash" )).isNull(); }
@Test public void testExplicitlyEnabledWithDefaults() { this.customizer.logstash.enabled(true); this.customizer.customize(); CustomHandler handler = this.customizer.logging.subresources().customHandler("logstash-handler"); assertThat( handler ).isNotNull(); Map props = handler.properties(); assertThat( props.get("hostname") ).isEqualTo( DEFAULT_HOSTNAME ); assertThat( props.get("port") ).isEqualTo( "" + DEFAULT_PORT ); CustomFormatter formatter = this.customizer.logging.subresources().customFormatter("logstash"); assertThat( formatter ).isNotNull(); }
@Test public void testExplicitlyEnabledWithLevelConfigValue() { this.customizer.logstash.level(Level.DEBUG); this.customizer.logstash.enabled(true); this.customizer.customize(); assertThat(customizer.logging.subresources().customHandler("logstash-handler").level()) .isEqualTo(Level.DEBUG); }
@Test public void testImplicitlyEnabledWithHostnameConfigValue() { this.customizer.logstash.hostname( "logstash.mycorp.com"); assertThat( this.customizer.logstash.enabled() ).isTrue(); this.customizer.customize(); CustomHandler handler = this.customizer.logging.subresources().customHandler("logstash-handler"); assertThat( handler ).isNotNull(); Map props = handler.properties(); assertThat( props.get("hostname") ).isEqualTo( "logstash.mycorp.com" ); assertThat( props.get("port") ).isEqualTo( "" + DEFAULT_PORT ); CustomFormatter formatter = this.customizer.logging.subresources().customFormatter("logstash"); assertThat( formatter ).isNotNull(); }
@Test public void testImplicitlyEnabledWithPortConfigValue() { this.customizer.logstash.port( 8675 ); assertThat( this.customizer.logstash.enabled() ).isTrue(); this.customizer.customize(); CustomHandler handler = this.customizer.logging.subresources().customHandler("logstash-handler"); assertThat( handler ).isNotNull(); Map props = handler.properties(); assertThat( props.get("hostname") ).isEqualTo( DEFAULT_HOSTNAME ); assertThat( props.get("port") ).isEqualTo( "8675" ); CustomFormatter formatter = this.customizer.logging.subresources().customFormatter("logstash"); assertThat( formatter ).isNotNull(); }
@Test public void testImplicitlyEnabledWithHostnameFractionSetting() { this.customizer.logstash.hostname( "logstash.mycorp.com"); this.customizer.customize(); assertThat( this.customizer.logstash.enabled() ).isTrue(); CustomHandler handler = this.customizer.logging.subresources().customHandler("logstash-handler"); assertThat( handler ).isNotNull(); Map props = handler.properties(); assertThat( props.get("hostname") ).isEqualTo( "logstash.mycorp.com" ); assertThat( props.get("port") ).isEqualTo( "" + DEFAULT_PORT ); CustomFormatter formatter = this.customizer.logging.subresources().customFormatter("logstash"); assertThat( formatter ).isNotNull(); }
@Test public void testAddingLogstashHandlerToExistingRootHandlers() { this.customizer.logging.rootLogger(Level.INFO, "HANDLER1", "HANDLER2"); this.customizer.logstash.enabled(true); this.customizer.customize(); assertThat(customizer.logging.subresources().rootLogger().handlers()) .contains("HANDLER1", "HANDLER2", "logstash-handler"); }
@Test public void testHonoringExistingRootLoggerLevel() { this.customizer.logging.rootLogger(Level.WARN, "HANDLER"); this.customizer.logstash.enabled(true); this.customizer.customize(); assertThat(customizer.logging.subresources().rootLogger().level()) .isEqualTo(Level.WARN); }