SpringBoot3:MyBatisPlus日志如何打印到.log文件中

网上的大多数做法是对MybatisPlus增加日志输出配置

mybatis-plus: 
	configuration: 
		log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

这样做确实可以将其打印出来,可以在idea的控制台查看:

SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@15b67b] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@6761653 wrapping oracle.jdbc.driver.T4CConnection@1ea226b] will not be managed by Spring
JsqlParserCountOptimize sql=SELECT id,password,address,phone,name,email FROM T_USER_TEST3  WHERE name = ?
==>  Preparing: SELECT COUNT(1) FROM T_USER_TEST3 WHERE name = ? 
==> Parameters: zhangsan3(String)
<==    Columns: COUNT(1)
<==        Row: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@15b67b]

was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@6761653 wrapping oracle.jdbc.driver.T4CConnection@1ea226b] will not be managed by Spring
这里会打印这个警告是因为没有加@Transactional注解,加上就行了

但是并不能输出到日志文件中

10-19 17:40:09.098 [main] DEBUG com.xmzn.ServiceWineMiniProgramApplication - Running with Spring Boot v3.1.4, Spring v6.0.12
10-19 17:40:09.099 [main] INFO  com.xmzn.ServiceWineMiniProgramApplication - No active profile set, falling back to 1 default profile: "default"
10-19 17:40:09.864 [main] INFO  o.s.d.r.config.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode
10-19 17:40:09.865 [main] INFO  o.s.d.r.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data Redis repositories in DEFAULT mode.
10-19 17:40:09.895 [main] INFO  o.s.d.r.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 21 ms. Found 0 Redis repository interfaces.
10-19 17:40:10.367 [main] INFO  o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 9999 (http)
10-19 17:40:10.443 [main] INFO  o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 1313 ms
10-19 17:40:11.170 [main] WARN  o.s.core.LocalVariableTableParameterNameDiscoverer - Using deprecated '-debug' fallback for parameter name resolution. Compile the affected code with '-parameters' instead or avoid its introspection: com.xmzn.utils.RedisUtil
10-19 17:40:12.605 [main] INFO  o.s.b.actuate.endpoint.web.EndpointLinksResolver - Exposing 1 endpoint(s) beneath base path '/actuator'
10-19 17:40:12.698 [main] INFO  o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 9999 (http) with context path ''
10-19 17:40:12.712 [main] INFO  com.xmzn.ServiceWineMiniProgramApplication - Started ServiceWineMiniProgramApplication in 4.042 seconds (process running for 4.526)
10-19 17:40:12.991 [RMI TCP Connection(3)-10.10.11.181] INFO  org.springframework.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
10-19 17:40:12.993 [RMI TCP Connection(3)-10.10.11.181] INFO  org.springframework.web.servlet.DispatcherServlet - Completed initialization in 1 ms

可以看到并没有输出数据库的日志

其实StdOutImpl只是把日志打印到控制台,看下StdOutImpl的源码所在的包,是用System.out.println打印的日志,显然这个是把日志打印到控制台,并不会存到日志文件。

在这里插入图片描述

SpringBoot默认使用的是logback日志组件,mybatis-plus同时提供了slf4j的日志实现,这个正是我们要用的。

在这里插入图片描述

修改Mybatis-plus的配置:

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl

重启再试,日志就打印出来了

在这里插入图片描述

低版本的话可能还需要再添加上对mybatis-plus和对应mapper的打印级别:

logging:
  level:
    com.baomidou.mybatisplus: debug
    com.*.mapper: debug

打印SQL可能对磁盘读写有一定的损耗,项目大的话不建议生产环境使用。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值