基于Springboot使用logback的注意事项

网友投稿 352 2022-10-08


基于Springboot使用logback的注意事项

Springboot logback的注意事项

项目使用SpringBoot搭建的,开发环境没有发现问题,日志输出位置也正常。

项目的日志没有使用默认配置文件名方式,而是一个环境一套配置文件,所以日志也是通过application.properties配置中间接指定的;

比如开发环境:

application.properties文件配置为:spring.profiles.active=dev

application-dev.properties文件的日志配置:logging.config=classpath:logback-dev.xml

但是在生产环境的时候发现启动项目会输出多个日志文件???

通过查看SpringBoot源码org.springframework.boot.context.logging.LoggingApplicationListener日志处理模块,在org.springframework.boot.logging.logback.LogbackLoggingSystem对象中找到加载默认配置文件的代码:

@Override

protected String[] getStandardConfigLocations() {

return new StedaWlring[] { "logback-test.groovy", "logback-test.xml", "logback.groovy", "logback.xml" };

}

生产配置为:

application.properties文件配置为:spring.profiles.active=prod

application-prod.properties文件的日志配置:logging.config=classpath:logback-prod.xml

看似没有问题,其实我的配置文件中还包含一套测试环境的配置文件,刚好其中的日志配置文件名是logback-test.xml与默认加载的配置文件名正好相同。

找到原因后将生产环境中的logback-test.xml删除掉日志输出就正常了。http://

结论:

如果项目中有多套环境是日志文件的名千万不要和默认配置文件名相同。

最后看了一下log4j的加载的源码,贴出来看看,多套环境时避免使用:

private String[] getCurrentlySupportedConfigLocations() {

List supportedConfigLocations = new ArrayList<>();

supportedConfigLocations.add("log4j2.properties");

if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) {

Collections.addAll(supportedConfigLocations, "log4j2.yaml", "log4j2.yml");

}

if (isClassAvailable("com.fasterxml.jackson.databind.ObjectMapper")) {

Collections.addAll(supportedConfigLocations, "log4j2.json", "log4j2.jsn");

}

supportedConfigLocations.add("log4j2.xml");

return StringUtils.toStringArray(supportedConfigLocations);

}

springboot使用logback会遇到的坑

Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/C:/Users/fyk/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.7/log4j-slf4j-impl-2.7.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.apache.logging.slf4j.Log4jLoggerFactory

at org.springframework.util.Assert.instanceCheckFailed(Assert.java:389)

at org.springframework.util.Assert.isInstanceOf(Assert.java:327)

at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:274)

at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:98)

at org.springframework.boot.logging.LoggingApplicationListener.onApplicationStartingEvent(LedaWloggingApplicationListener.java:230)

at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:209)

at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)

at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)

at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:69)

at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:292)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)

at com.light.SpringbootApplication.main(SpringbootApplication.java:32)

... 5 more

在spring boot中导入logback jar包会与spring-boot-starter-web冲突,应该是springboot中已经包含了这个包,

ch.qos.logback

logback-classic

1.2.3

去掉这个导入就好了。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Windows 2008阿里云服务器IP地址和MAC地址绑定(windows xp)
下一篇:Juniper SRX550防火墙之基本配置(juniper是什么品牌)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~