And*_*eas 10 java logging spring wso2carbon spring-boot
Spring Boot使用自动初始化基础日志系统LoggingApplicationListener
.如果我正在开发的应用程序是孤立的或独立的,那么这是一件好事.
但是我正在开发一个Web应用程序,它将部署到WSO2 Application Server中,它提供统一的日志记录(使用log4j),具有中央日志级别管理(运行时通过Web界面),业务报告等功能.
如果我"按原样"使用Spring Boot,它会完全记录所有内容.我的第一个镜头是,删除spring-boot-starter-logging
并手动添加slf4j-api
为provided
.这在一定程度上起作用,因为LoggingApplicationListener
现在覆盖了WSO2提供的全局logmanager的设置(甚至导致全局appender被关闭).
我想出的唯一"解决方案"是通过反射删除监听器.然后Spring Boot开始表现得完全正常(通过全局记录器记录,而不是覆盖预定义的日志级别,输出格式,appender等)
那个"解决方案"看起来像这样:
@SpringBootApplication
public class MyApp extends SpringBootServletInitializer {
public static void main(String... args) {
SpringApplication.run(MyApp.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
try {
Field appField = SpringApplicationBuilder.class.getDeclaredField("application");
appField.setAccessible(true);
SpringApplication app = (SpringApplication)appField.get(builder);
Field listenersField = SpringApplication.class.getDeclaredField("listeners");
listenersField.setAccessible(true);
List<ApplicationListener<?>> listeners = (List<ApplicationListener<?>>) listenersField.get(app);
for (int i = listeners.size() - 1; i >= 0; --i) {
if (listeners.get(i) instanceof LoggingApplicationListener) {
listeners.remove(i);
}
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return builder.sources(MyApp.class);
}
}
Run Code Online (Sandbox Code Playgroud)
对于我的问题有没有更好的解决方案,在我的研究和代码分析中,我可能忽略了这些问题?
小智 6
谢谢你的帖子,这很有帮助。我在 Websphere 应用程序服务器上遇到了同样的问题:在 spring boot 上下文初始化之后,我没有更多的日志。通过覆盖 SpringBootServletInitializer 的 run 方法,此解决方案等效但不那么脏:
@Override
protected WebApplicationContext run(SpringApplication application) {
Collection<ApplicationListener<?>> listeners =
new ArrayList<>();
for (ApplicationListener<?> listener: application.getListeners()) {
if (!(listener instanceof LoggingApplicationListener)) {
listeners.add(listener);
}
}
application.setListeners(listeners);
return super.run(application);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
从 Spring Boot 1.4 开始,可以禁用 LoggingSystem 自动配置。
\n\n看一下Spring 文档的自定义日志配置部分:
\n\n\n\n\n您可以使用系统属性强制 Spring Boot 使用特定的日志系统
\norg.springframework.boot.logging.LoggingSystem
。该值应该是实现的完全限定类名LoggingSystem
。您还可以使用 值完全禁用 Spring Boot\xe2\x80\x99s 日志记录配置none
。
以Tomcat为例,设置环境变量JAVA_OPTS
:
JAVA_OPTS="-Dorg.springframework.boot.logging.LoggingSystem=none"\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
2735 次 |
最近记录: |