#StackBounty: #spring #spring-cloud #spring-logback #logstash-logback-encoder #spring-autoconfiguration Spring boot remove logback appe…

Bounty: 50

I’d like to have spring auto configuration which adds LogstashTcpSocketAppender.

What I have done:

  1. The LogstashTcpSocketAppender was added in to the LoggerContext from the LogstashAutoConfiguration.java
@Configuration
@ConditionalOnProperty(name = "logging.logstash.url")
@RequiredArgsConstructor
public class LogstashAutoConfiguration {
    
    @Value("${spring.application.name:null}")
    private String applicationName;
    
    @Value("${logging.logstash.url}")
    private String logstashUrl;
    
    @Bean
    public LogstashTcpSocketAppender logstashAppender() {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

        LogstashTcpSocketAppender logstashTcpSocketAppender = new LogstashTcpSocketAppender();
        logstashTcpSocketAppender.setName("LOGSTASH");
        logstashTcpSocketAppender.setContext(loggerContext);
        logstashTcpSocketAppender.addDestination(logstashUrl);

        LogstashEncoder encoder = new LogstashEncoder();
        encoder.setIncludeMdc(true);
        encoder.getFieldNames().setLevelValue(null);
        encoder.setCustomFields(String.format("{"app_name":"%s"}", applicationName));

        logstashTcpSocketAppender.setEncoder(encoder);
        logstashTcpSocketAppender.start();

        loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).addAppender(logstashTcpSocketAppender);

        return logstashTcpSocketAppender;
    }
}

  1. After a while, the Spring Boot trigger a event will make the app reconfig. (for example I use Consul so I just change property in key/value storage, then spring refresh my context)
  2. It call the initializeWithConventions in the AbstractLoggingSystem.java
  3. then it will call the loadConfiguration in the LogbackLoggingSystem.java
  4. then it will stopAndReset(loggerContext). here it will stop all the appenders, and it will resetAllListeners();, which will clear the all logback listeners. (so I cannot use logback listeners for addition appenders again)

Are there correct way add Appender through spring auto configuration?
How can I prevent remove LogstashTcpSocketAppender from LoggerContext when spring makes the app reconfig?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.