2

我想要添加 LogstashTcpSocketAppender 的 spring 自动配置。

我做了什么:

  1. LogstashTcpSocketAppender 从 LogstashAutoConfiguration.java 添加到 LoggerContext
@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. 一段时间后,Spring Boot 触发一个事件将使应用程序重新配置。(例如,我使用 Consul,所以我只需更改键/值存储中的属性,然后刷新我的上下文)
  2. 它调用 AbstractLoggingSystem.java 中的 initializeWithConventions
  3. 然后它会调用 LogbackLoggingSystem.java 中的 loadConfiguration
  4. 然后它将停止AndReset(loggerContext)。在这里它会停止所有的appender,它会resetAllListeners();,这将清除所有的logback监听器。(所以我不能再次使用 logback 侦听器来添加附加程序)

是否有正确的方法通过 spring 自动配置添加 Appender?spring 重新配置应用程序时,如何防止从 LoggerContext 中删除 LogstashTcpSocketAppender?

4

1 回答 1

1

在上面的Configuration类中,目的是添加一个appenderforLogstash并负责发送日志。

如果环境变量发生变化或上下文被刷新,您可以监听相关事件,然后检查您的 Logstash appender 是否已配置。确保仅在缺少 Logstash 附加程序时才添加它。

这是会做同样事情的课程。

@Configuration
@ConditionalOnProperty(name = "logging.logstash.url")
public class LogstashAppenderConfiguration {

    @Value("${spring.application.name:null}")
    private String applicationName;

    @Value("${logging.logstash.url}")
    private String logstashUrl;

    @EventListener(ContextRefreshedEvent.class)
    public void onContextRefreshedEvent(ContextRefreshedEvent event) {
        this.addLogStashAppenderIfMissing();
    }

    @EventListener(RefreshScopeRefreshedEvent.class)
    public void onRefreshScopeRefreshedEvent(RefreshScopeRefreshedEvent event) {
        this.addLogStashAppenderIfMissing();
    }

    @EventListener(EnvironmentChangeEvent.class)
    public void onEnvironmentChangeEvent(EnvironmentChangeEvent event) {
        this.addLogStashAppenderIfMissing();
    }

    public void addLogStashAppenderIfMissing() {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        synchronized (this) {
            if (Objects.isNull(loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).getAppender("LOGSTASH"))) {
                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);                                                                                      ;
            }
        }
    }
}
于 2020-09-27T07:00:05.053 回答