上篇我认为我已经讲的很明白了 Springboot 兼容各日志框架的做法,但是还有网友在群里问我,微信公众号留言,因此我这里再花一篇时间来说明 SpringBoot 整合日志框架的方法。
各种框架遗留问题
Springboot 使用(slf4j+logback): Spring 使用(commons-logging)、Hibernate 使用(jboss-logging)、MyBatis 使用 (Apache Commons Logging)等。
统一日志记录,就是让别的框架和我一起统一使用slf4j进行输出?

如何让系统中所有的日志都统一到slf4j:
- 将系统中其他日志框架先排除出去;
- 用中间包来替换原有的日志框架;
- 我们导入slf4j其他的实现
SpringBoot日志关系
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
上面的 starter 默认会引入下面的日志 jar 包。SpringBoot使用它来做日志功能:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
底层依赖关系:

总结:
- SpringBoot底层也是使用slf4j+logback的方式进行日志记录
- SpringBoot也把其他的日志都替换成了slf4j;
- 中间替换包?
@SuppressWarnings("rawtypes")
public abstract class LogFactory {
static String UNSUPPORTED_OPERATION_IN_JCL_OVER_SLF4J = "http://www.slf4j.org/codes.html#unsupported_operation_in_jcl_over_slf4j";
static LogFactory logFactory = new SLF4JLogFactory();

如果我们要引入其他框架?一定要把这个框架的默认日志依赖移除掉!Spring框架用的是commons-logging:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency>
SpringBoot能自动适配所有的日志,而且底层使用slf4j+logback的方式记录日志,引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉即可。

: » SpringBoot 兼容各日志框架的方法
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/251781.html