SpringBoot 其他日志框架统一转为slf4j
2019-06-05 本文已影响29人
喊我小王吧
SpringBoot 其他日志框架统一转为slf4j
SpringBoot 版本 2.1.0.M2
通过查看springboot 的日志依赖流程图可以发现
idea中可以在pom.xml 右键查看依赖关系图

springboot中日志使用关系
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.1.0.M2</version>
<scope>compile</scope>
</dependency>
我们在使用springboot 时候,默认采用此日志门面输出日志。有时候其他框架不一定适用springboot 日志 , 不同的框架底层使用的日志框架也是不尽相同。
日志会产生冲突,我们需要统一日志输出门面
上图依赖流程图中可以看出由 springboot 默认Slf4j 日志门面,采用日志是Slf4j + logback框架 来进行日志输出
通过查询slf4j 官网可以看到,将其他日志框架转换为slf4j所需要的依赖jar包,也就是中间包
slf4j api 文档
总结:
1 springboot 底层采用slf4j + logback 日志实现方式进行日志记录
2 还导入一些 其他xxx-to-xxx 依赖 用来替换其他日志,srpingboot 将其它日志框架转换为slf4j
如果我们引入其他日志框架? 一定要将日志框架依赖移除!
springboot为我们提供了相应的解决办法
我们手动移除日志依赖,添加jar包,其实不用springboot为我们已经做到了,上面依赖关系图中springboot已经引入这些中间包来将其他日志门面转为slf4j日志门面。
1 将系统中其他框架排除出去
例如: spring 默认日志框架是 common-logging 日志框架
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
2 用中间包将日志进行替换
springboot默认已经导入jar依赖
3 导入slf4j其他的实现即可
根据自己选择导入日志依赖(一般默认用logback即可,根据自己喜好)
其实就是下面一句话
srpingboot能自动适配其他日志框架,而且底层采用slf4j + logback 日志输出
我们所做的就是移除其他框架的日志即可,springboot已经帮我们进行转换了