基于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对象中找到加载默认配置文件的代码:

@Overrideprotected String[] getStandardConfigLocations() { return new String[] { "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删除掉日志输出就正常了。

结论:

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

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

private String[] getCurrentlySupportedConfigLocations() { List<String> 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(LoggingApplicationListener.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中已经包含了这个包,

   <dependency>   <groupId>ch.qos.logback</groupId>   <artifactId>logback-classic</artifactId>   <version>1.2.3</version>  </dependency>

去掉这个导入就好了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

未曾失败的人恐怕也未曾成功过。

基于Springboot使用logback的注意事项

相关文章:

你感兴趣的文章:

标签云: