Spring引导删除代码中添加的logback appender(自动配置)



我想进行spring自动配置,添加LogstashTcpSocketAppender。

我所做的:

  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(。在这里,它将停止所有的appenders,并将resetAllListeners((;,这将清除所有的logback侦听器。(因此,我不能再次使用logback侦听器来添加附加程序(

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

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

如果环境变量发生更改或上下文被刷新,您可以监听相关事件,然后检查是否配置了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);                                                                                      ;
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新