使用spring-mybatis进行Spring-boot - 如何强制它记录所有SQL查询 [英] Spring-boot with spring-mybatis - how to force it to logging all SQL queries

查看:119
本文介绍了使用spring-mybatis进行Spring-boot - 如何强制它记录所有SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的spring-boot-mybatis应用程序(请记住,请)。 Mybatis仅在发生故障时(在例外情况下)记录SQL查询。请告诉我,如何强制它将所有SQL查询记录到控制台?

I have a simple spring-boot-mybatis app (keep in mind, please). Mybatis is logging SQL queries only in case of failure (on excepions). Tell me please, how to force it to log all SQL query to console ?

此时我正在使用 slf4j logger(由 spring-boot <自动配置/ code>)。

我找到这个链接: http ://www.mybatis.org/mybatis-3/logging.html

但是我没有设法遵循它。首先显示 log4j 的配置,我不确定如果我正确理解:在 application.properties

At this moment I am using slf4j logger (automatically configured by spring-boot).
I find this link: http://www.mybatis.org/mybatis-3/logging.html
however I didnt manage to follow it. First of all configuration is shown for log4j, and I am not sure If I correctly understand: Is it sufficient to configure in application.properties ?

提前致谢

推荐答案

Spring启动使用logback作为默认日志记录Slf4j的提供者。 Ibatis内部日志工厂加载SLF4j作为首选记录器。您所要做的就是配置Spring启动记录器以发布ibatis映射器的日志消息。

Spring boot uses logback as default logging provider for Slf4j. Ibatis internal log factory loads the SLF4j as the first choice logger. All you have to do is configure your spring boot logger to publish log messages for ibatis mapper.

在启动应用程序属性中添加以下行。

Add the below lines in boot application properties.

logging.level.org.springframework=WARN
logging.level.com.spring.ibatis.UserMapper=DEBUG
logging.file=logs/spring-boot-logging.log

第二行是您定义日志条目的位置具有DEBUG日志级别的ibatis映射器。 com.spring.ibatis 是包, UserMapper 是样本映射器。

The second line is where you define the logging entry for ibatis mapper with DEBUG log level. com.spring.ibatis is package and the UserMapper is sample mapper.

以下日志将开始出现在控制台和spring-boot-logging文件中。这些是从 saveUser findByName 生成的日志消息 ApplicationTest class。

The following logs will start to appear in the console and in the spring-boot-logging file. These are the log messages generated from saveUser and findByName method of ApplicationTest class.

2016-12-19 22:07:06.358  INFO 7248 --- [main] com.spring.ibatis.ApplicationTest        : Started ApplicationTest in 3.048 seconds (JVM running for 4.209)
2016-12-19 22:07:06.424 DEBUG 7248 --- [main] com.spring.ibatis.UserMapper.saveUser    : ==>  Preparing: insert into users(name) values(?) 
2016-12-19 22:07:06.444 DEBUG 7248 --- [main] com.spring.ibatis.UserMapper.saveUser    : ==> Parameters: ibatis(String)
2016-12-19 22:07:06.445 DEBUG 7248 --- [main] com.spring.ibatis.UserMapper.saveUser    : <==    Updates: 1
2016-12-19 22:07:06.457 DEBUG 7248 --- [main] com.spring.ibatis.UserMapper.findByName  : ==>  Preparing: select name from users WHERE name=? 
2016-12-19 22:07:06.470 DEBUG 7248 --- [main]  com.spring.ibatis.UserMapper.findByName  : ==> Parameters: ibatis(String)
2016-12-19 22:07:06.504 DEBUG 7248 --- [main]  com.spring.ibatis.UserMapper.findByName  : <==      Total: 1

您当然可以配置任何您想要的记录器选项。如果需要,我可以轻松地为任何其他记录器添加一个示例。

You can of course configure any choice of logger you want. I can easily add an example for any other logger should you need.

您可以在
找到包含Junit测试用例的完整代码 https://github.com/saagar2000/ibatis

这篇关于使用spring-mybatis进行Spring-boot - 如何强制它记录所有SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆