基于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 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 1.5.4 集成 jpa+hibernate+jdbcTemplate(详解)
下面小编就为大家带来一篇基于spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-06-06Java微信二次开发(二) Java微信文本消息接口请求与发送
这篇文章主要为大家详细介绍了Java微信二次开发第二篇,Java微信文本消息接口请求与发送功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-04-04java自带命令行工具jmap、jhat与jinfo的使用实例代码详解
本篇文章主要通过代码实例对java自带命令行工具jmap、jhat与jinfo的使用做出了详解,需要的朋友可以参考下2017-04-04Java语言Consistent Hash算法学习笔记(代码示例)
这篇文章主要介绍了Java语言Consistent Hash算法学习笔记(代码示例),分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下2018-02-02
最新评论