欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

基于Springboot使用logback的注意事项

时间:2021-12-28 10:42:32|栏目:JAVA代码|点击:

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 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>

去掉这个导入就好了。

上一篇:spring boot使用logback日志级别打印控制操作

栏    目:JAVA代码

下一篇:Java 8 中 Map 骚操作之 merge() 的使用方法

本文标题:基于Springboot使用logback的注意事项

本文地址:http://www.codeinn.net/misctech/188280.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有