Spring Boot:LoggingApplicationListener干扰Application Server日志记录 [英] Spring Boot: LoggingApplicationListener interfering with Application Server logging

查看:205
本文介绍了Spring Boot:LoggingApplicationListener干扰Application Server日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Boot使用 LoggingApplicationListener 自动初始化基础日志系统。如果我正在开发的应用程序是独立运行或独立运行,这是一件好事。

Spring Boot automatically initializes the underlying logging system using the LoggingApplicationListener. This is a nice thing if the application I'm developing runs isolated or standalone.

但是我正在开发一个将部署到WSO2 Application Server中的Web应用程序,它提供统一日志记录(使用log4j),具有中央日志级别管理(运行时通过Web界面),业务报告等功能。

However I'm developing a web application that will be deployed into the WSO2 Application Server, which offers unified logging (using log4j), with features like central log level management (at runtime via web interface), business reporting etc.

如果我使用Spring Boot是,它完全记录了一切。我的第一个镜头是删除 spring-boot-starter-logging 并手动添加 slf4j-api 提供。这在某种程度上起作用,因为 LoggingApplicationListener 现在覆盖了WSO2提供的全局logmanager的设置(甚至导致全局appender被关闭)。

If I use Spring Boot "as is", it logs everything completely on its own. My first shot was, to remove spring-boot-starter-logging and manually add slf4j-api as provided. This works to some extent, since the LoggingApplicationListener now overrides settings of the global logmanager provided by WSO2 (and even causes global appenders to be closed).

我想出的唯一解决方案是通过反射删除监听器。然后Spring Boot开始表现得完全正常(通过全局记录器记录而不是覆盖预定义的日志级别,输出格式,appender等)。

The only "solution" I came up with is to remove the listener via reflection. Then Spring Boot starts to behave exactly as it should (logging via the global logger and not overriding the pre defined log levels, output formats, appenders, etc.)

那个解决方案看起来像这样:

That "solution" looks like this:

@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);
    }
}

我的问题有没有更好的解决方案可能更少在我的研究和代码分析中我可能忽略了hacky?

Is there any better solution to my problem that's maybe less hacky which I may have overlooked during my research and code analysis?

推荐答案

谢谢你发帖它非常有帮助。
我遇到了与Websphere Aplication Server相同的问题:初始启动上下文初始化后,我没有更多日志。通过覆盖SpringBootServletInitializer的run方法,此解决方案相当但不那么脏:

thank you for you post it is very helpful. I had the same problem with Websphere Aplication Server: After spring boot context initialized I had no more logs. This solution is equivalent but less dirty by overriding the run method of SpringBootServletInitializer:

@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);
    }

这篇关于Spring Boot:LoggingApplicationListener干扰Application Server日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆