• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

基于Springboot使用logback的注意事项

springboot 搞代码 4年前 (2022-01-05) 29次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了Springboot使用logback的注意事项,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Springboot logback的注意事项

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

来源gaodai^.ma#com搞#代!码网

项目的日志没有使用默认配置文件名方式,而是一个环境一套配置文件,所以日志也是通过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 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中已经包含了这个包,

  ch.qos.logbacklogback-classic1.2.3

去掉这个导入就好了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网

以上就是基于Springboot使用logback的注意事项的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:基于Springboot使用logback的注意事项

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址