springboot默认的日志抽象层框架是:SLF4j
springboot默认的日志实现框架是: Logback
Spring框架默认是用JCL(commons-logging)作为日志抽象层框架;
springboot在集成spring、mybatis等框架的过程中,已经帮我们排除了其他框架的原本默认日志,因此我们不需要再进行依赖排除
如何让系统中所有的日志都统一到slf4j:
1、将系统中其他日志框架先排除出去;
2、用中间包来替换原有的日志框架;
3、我们导入slf4j其他的实现
上面是springbootr日志的一些转化,下面来说如何使用logback日志记录数据库操作:
application.yml文件中进行两个配置:
logging:
config: classpath:logback.xml
level:
com.example.dao: debug #指定dao包下文件的日志级别
logback.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds">
<!--日志输入到控制台-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="error"> <!--指定整个项目的默任日志级别为 error -->
<appender-ref ref="STDOUT" />
</root>
</configuration>
2020-12-19 14:07:09.419 [http-nio-8080-exec-1] DEBUG com.example.dao.IStaffnterfaceDao.selectByid - ==> Preparing: select * from staff where sid=?
2020-12-19 14:07:09.440 [http-nio-8080-exec-1] DEBUG com.example.dao.IStaffnterfaceDao.selectByid - ==> Parameters: 3(Integer)
2020-12-19 14:07:09.458 [http-nio-8080-exec-1] DEBUG com.example.dao.IStaffnterfaceDao.selectByid - <== Total: 1
此时,运行测试,控制台仅显示数据库操作的日志。