1、mybatis plus打印sql日志
参考文档:mybatis plus打印sql日志_mybatisplus日志打印-CSDN博客
2、修改
修改InfoLevelLogger
@Override
public void debug(String s) {// 修改这里logger.info(s);log.debug(s);
}
- 增加:log.debug(s);
修改logback.xml
<!-- 新增SQL日志输出 -->
<appender name="SqlFile" class="ch.qos.logback.core.rolling.RollingFileAppender"><File>${LOG_HOME:-d:/}logs/sql/sql.log</File><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><FileNamePattern>${LOG_HOME:-d:/}logs/sql/sql-%d{yyyy-MM-dd}.%i.log</FileNamePattern><MaxHistory>60</MaxHistory><TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><MaxFileSize>5MB</MaxFileSize></TimeBasedFileNamingAndTriggeringPolicy></rollingPolicy><layout class="ch.qos.logback.classic.PatternLayout"><pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern></layout>
</appender><!-- 配置com.ybw.interceptor的日志输出 -->
<logger name="com.ybw.interceptor" level="debug" additivity="false"><appender-ref ref="SqlFile" />
</logger>
- 将sql日志文件单独打印到 ${LOG_HOME:-d:/}logs/sql目录下。
解释
- InfoLevelLogger 的作用:
- InfoLevelLogger 是一个自定义的 MyBatis 日志实现,目的是将 MyBatis 的日志级别从 DEBUG 改为 INFO。
- 在 debug 方法中,logger.info(s) 是通过 LoggerFactory.getLogger(clazz) 创建的 Logger 实例,而 log.debug(s) 是通过 Lombok 的 @Slf4j 注解生成的 Logger 实例。
- logback.xml 的配置
- 在 logback.xml 中配置了日志输出目录为 sql,这意味着只有符合该配置的日志才会输出到 sql 目录。
- logger.info(s) 使用的是 LoggerFactory.getLogger(clazz),其日志输出路径由 clazz 决定。示例如下:
[INFO ] 2025-04-18 17:11:37.917 [main] org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession [INFO ] 2025-04-18 17:11:37.927 [main] org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1fca53a7] was not registered for synchronization because synchronization is not active [INFO ] 2025-04-18 17:11:38.017 [main] com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting... [INFO ] 2025-04-18 17:11:38.480 [main] com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@23365142 [INFO ] 2025-04-18 17:11:38.484 [main] com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed. [INFO ] 2025-04-18 17:11:38.498 [main] o.m.s.t.SpringManagedTransaction - JDBC Connection [HikariProxyConnection@1729753606 wrapping com.mysql.cj.jdbc.ConnectionImpl@23365142] will not be managed by Spring [INFO ] 2025-04-18 17:11:38.514 [main] c.ybw.mapper.UserMapper.updateById - ==> Preparing: UPDATE user SET `no`=?, update_time=? WHERE id=? [INFO ] 2025-04-18 17:11:38.575 [main] c.ybw.mapper.UserMapper.updateById - ==> Parameters: 16(Integer), 2025-04-18T17:11:37.902300800(LocalDateTime), 1(Long) [INFO ] 2025-04-18 17:11:38.591 [main] c.ybw.mapper.UserMapper.updateById - <== Updates: 1 [INFO ] 2025-04-18 17:11:38.594 [main] org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1fca53a7]
- log.debug(s) 使用的是 Lombok 的 @Slf4j 注解生成的 Logger,其日志输出路径由 InfoLevelLogger 的包路径(com.ybw.interceptor)决定。示例如下:
[DEBUG] 2025-04-18 17:11:34.719 [main] com.ybw.interceptor.InfoLevelLogger - Logging initialized using 'class com.ybw.interceptor.InfoLevelLogger' adapter. [DEBUG] 2025-04-18 17:11:34.990 [main] com.ybw.interceptor.InfoLevelLogger - Get /10.2.5.21 network interface [DEBUG] 2025-04-18 17:11:34.991 [main] com.ybw.interceptor.InfoLevelLogger - Get network interface info: name:wireless_32768 (Realtek RTL8852BE WiFi 6 802.11ax PCIe Adapter) [DEBUG] 2025-04-18 17:11:35.023 [main] com.ybw.interceptor.InfoLevelLogger - Initialization Sequence datacenterId:22 workerId:20 [DEBUG] 2025-04-18 17:11:37.919 [main] com.ybw.interceptor.InfoLevelLogger - Creating a new SqlSession [DEBUG] 2025-04-18 17:11:37.928 [main] com.ybw.interceptor.InfoLevelLogger - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1fca53a7] was not registered for synchronization because synchronization is not active [DEBUG] 2025-04-18 17:11:38.498 [main] com.ybw.interceptor.InfoLevelLogger - JDBC Connection [HikariProxyConnection@1729753606 wrapping com.mysql.cj.jdbc.ConnectionImpl@23365142] will not be managed by Spring [DEBUG] 2025-04-18 17:11:38.514 [main] com.ybw.interceptor.InfoLevelLogger - ==> Preparing: UPDATE user SET `no`=?, update_time=? WHERE id=? [DEBUG] 2025-04-18 17:11:38.575 [main] com.ybw.interceptor.InfoLevelLogger - ==> Parameters: 16(Integer), 2025-04-18T17:11:37.902300800(LocalDateTime), 1(Long) [DEBUG] 2025-04-18 17:11:38.592 [main] com.ybw.interceptor.InfoLevelLogger - <== Updates: 1 [DEBUG] 2025-04-18 17:11:38.594 [main] com.ybw.interceptor.InfoLevelLogger - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1fca53a7]
- 从logger.info(s)、log.debug(s)打印的日志可以看出,内容虽然一样,但打印的类是不一样的。
3、源代码
share: 分享仓库 - Gitee.com