Vin*_*t F 3 java logback spring-boot
我正在使用 Spring Boot 1.5.4 并且我正在尝试编写一个启动器,其中包含用于使用它的任何应用程序的自定义日志附加程序。这是我注册它的方式:
@Override
synchronized public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
if (!addedCustomAppender && metricProperties.isEnabled()) {
//Add log appender for Monitoring
EventsPublisher eventsPublisher = contextRefreshedEvent.getApplicationContext().getBean(EventsPublisher.class);
final Appender<ILoggingEvent> newAppender = new CustomLogbackAppender(eventsPublisher);
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
final Logger root = context.getLogger("ROOT");
newAppender.setName("Custom Appender");
newAppender.setContext(context);
newAppender.start();
root.addAppender(newAppender);
addedCustomAppender = true;
}
}
Run Code Online (Sandbox Code Playgroud)
这被调用,我可以看到 ROOT 记录器具有附加程序。它实际上在应用程序启动期间工作了几秒钟。
但是很快,在应用程序启动的后期,一个 ApplicationEnvironmentPreparedEvent 被触发,这会触发 LoggingApplicationListener 中的一种重新初始化。
这发生在我在日志中看到后:
INFO 15888 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase -2147482648
Run Code Online (Sandbox Code Playgroud)
在 LogbackLoggingSystem 中,我可以看到对 stopAndReset 方法的调用,该方法调用记录器上的 recursiveReset 方法。之后,Spring 默认配置被加载,我的自定义 appender 不再存在。
如何避免这种情况发生,以便我的自定义 appender 尽早获取一些日志并保持连接到 ROOT logger ?
或者至少在一切都初始化后如何注册我的 appender?(在那种情况下,我想我会错过一些日志,所以这不是我的目标解决方案)
谢谢 !
春天LoggingApplicationListener是……
Ordered.HIGHEST_PRECEDENCE + 20ApplicationStartingEvent.class, ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class, ContextClosedEvent.class, ApplicationFailedEvent.class因此,LoggingApplicationListener绑定到(即响应)与您的侦听器 ( ContextRefreshedEvent)相同的应用程序事件,并且由于它以如此高的优先级 ( Ordered.HIGHEST_PRECEDENCE + 20) 运行,因此它通过在之后运行来取代您的侦听器。
您需要更改侦听器的优先级以“保持连接到 ROOT 记录器”并绑定到不同的事件以“尽早获取一些日志”。
这是一个示例实现:
public class LoggingConfigurationApplicationListener implements GenericApplicationListener {
private static final Logger logger = LoggerFactory.getLogger(LoggingConfigurer.class);
private boolean addedCustomAppender;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (!addedCustomAppender && metricProperties.isEnabled()) {
ApplicationPreparedEvent applicationEvent = (ApplicationPreparedEvent) event;
EventsPublisher eventPublisher = applicationEvent.getApplicationContext().getBean(EventsPublisher.class);
//Add log appender for Monitoring
final Appender<ILoggingEvent> newAppender = new CustomLogbackAppender(eventPublisher);
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
final ch.qos.logback.classic.Logger root = context.getLogger("ROOT");
newAppender.setName("Custom Appender");
newAppender.setContext(context);
newAppender.start();
root.addAppender(newAppender);
logger.info("Added custom log appender");
addedCustomAppender = true;
}
}
@Override
public int getOrder() {
// this must be higher than LoggingApplicationListener.DEFAULT_ORDER
return Ordered.HIGHEST_PRECEDENCE + 21;
}
@Override
public boolean supportsEventType(ResolvableType eventType) {
// this is the earliest 'event type' which is capable of exposing the application context
return ApplicationPreparedEvent.class.isAssignableFrom(eventType.getRawClass());
}
@Override
public boolean supportsSourceType(Class<?> sourceType) {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以按如下方式将此侦听器与 Spring Boot 应用程序相关联:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.listeners(new LoggingConfigurationApplicationListener())
.run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
使用此实现,您将尽早重新配置 LoggerContext(当 SpringApplication 启动并且所有 bean 定义已加载时)并且您的更改不会被 Spring 的LoggingApplicationListener.
以下是一些带有上述侦听器的示例日志,它们显示 (a) 正在添加的自定义 appender 和 (b) 在应用程序启动后自定义 appender 仍然存在:
2017-08-22 09:25:01|restartedMain|INFO |c.s.s.logging.LoggingConfigurer|Added custom log appender
2017-08-22 09:25:01|restartedMain|INFO |c.s.springboot.Application|Starting Application on ... with PID 92710 ...
2017-08-22 09:25:01|restartedMain|INFO |c.s.springboot.Application|No active profile set, falling back to default profiles: default
2017-08-22 09:25:07|restartedMain|INFO |o.a.catalina.core.StandardService|Starting service [Tomcat]
2017-08-22 09:25:07|restartedMain|INFO |o.a.catalina.core.StandardEngine|Starting Servlet Engine: Apache Tomcat/8.5.16
2017-08-22 09:25:07|localhost-startStop-1|INFO |o.a.c.c.C.[.[.[/spring-boot]|Initializing Spring embedded WebApplicationContext
2017-08-22 09:25:09|Thread-6|INFO |c.s.s.logging.LoggingVerifier|Custom appender is present
2017-08-22 09:25:10|Thread-6|INFO |c.s.s.logging.LoggingVerifier|Custom appender is present
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4378 次 |
| 最近记录: |