前提不使用配置logging.config
外的方法。
通过DEBUG源码自己实现了动态追加。
从网上百度logback重新加载能找到如下关键代码
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(null);
DEBUG期中doConfigure方法,能发现springboot启动方法调用栈核心类是LogbackLoggingSystem。
我们需要继承改类型重写initialize, reinitialize方法。
重写后怎么让springboot内置类使用我们自己的类呢。
继续看调用栈
发现LogginApplicationListener中变量loggingSystem。该变量创建关键代码在
点击进去代码如下
根据SYSTEM_PROPERTY系统变量来绝对创建的类,通过代码发现值是需要创建的类名
然后在springboot启动前执行一下
System.setProperty(LoggingSystem.SYSTEM_PROPERTY, MyLogbackLogginSystem.class.getName());
然后springboot就会创建我们自己的类了。
以下是业务重写逻辑,根据变量来确定是否追加可控制台输出