java 带有 spring-mybatis 的 Spring-boot - 如何强制它记录所有 SQL 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41001188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring-boot with spring-mybatis - how to force it to logging all SQL queries
提问by Sagar Veeram
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 ?
我有一个简单的 spring-boot-mybatis 应用程序(请记住)。Mybatis 仅在失败的情况下(在异常情况下)记录 SQL 查询。请告诉我,如何强制它将所有 SQL 查询记录到控制台?
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
?
此时我正在使用slf4j
记录器(由 自动配置spring-boot
)。
我找到了这个链接:http: //www.mybatis.org/mybatis-3/logging.html
但是我没有设法遵循它。首先显示的是 配置log4j
,我不确定我是否正确理解:在 中配置是否足够application.properties
?
Thanks in advance
提前致谢
回答by Sagar Veeram
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.
Spring Boot 使用 logback 作为 Slf4j 的默认日志记录提供程序。Ibatis 内部日志工厂加载 SLF4j 作为首选记录器。你所要做的就是配置你的spring boot logger来发布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
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.
第二行是您为具有 DEBUG 日志级别的 ibatis 映射器定义日志条目的地方。com.spring.ibatis
是包,UserMapper
是示例映射器。
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.
以下日志将开始出现在控制台和 spring-boot-logging 文件中。这些是从类saveUser
和findByName
方法生成的日志消息ApplicationTest
。
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.
您当然可以配置您想要的任何记录器选择。如果您需要,我可以轻松地为任何其他记录器添加一个示例。
You can find the complete code with Junit test cases at https://github.com/saagar2000/ibatis
您可以在https://github.com/saagar2000/ibatis找到带有 Junit 测试用例的完整代码
回答by Alan Hay
An alternative approach is to use a proxy driver such as log4jdbc2 which has the advantage of logging the exact SQL going to the database with parameters in place unlike the other answers. This will work regardless of the persistence abastraction layer (e.g. iBatis, JPA etc).
另一种方法是使用代理驱动程序,例如 log4jdbc2,它的优点是将准确的 SQL 记录到数据库中,并且参数到位,这与其他答案不同。无论持久化抽象层(例如 iBatis、JPA 等)如何,这都将起作用。
https://code.google.com/archive/p/log4jdbc-log4j2/
https://code.google.com/archive/p/log4jdbc-log4j2/
One major convenience of this is that you can copy the SQL straight to your DB front-end and execute as is.
这样做的一个主要便利是您可以将 SQL 直接复制到您的数据库前端并按原样执行。
1 Add Maven Dependencies:
1 添加Maven依赖:
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
<version>1.16</version>
</dependency>
2 Add logback configuration. Copy the relevant parts to your existing logback.xml
2 添加logback配置。将相关部分复制到您现有的 logback.xml
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="jdbc.audit" level="ERROR" />
<logger name="jdbc.connection" level="ERROR" />
<logger name="jdbc.sqltiming" level="ERROR" />
<logger name="jdbc.resultset" level="ERROR" />
<!-- UNCOMMENT THE BELOW TO HIDE THE RESULT SET TABLE OUTPUT -->
<!--<logger name="jdbc.resultsettable" level="ERROR" /> -->
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
3 Tell log4jdbc2 about your logging config:
3 告诉 log4jdbc2 你的日志配置:
Create a file named log4jdbc.log4j2.propertiesat the root of the classpath src/test/resourcesor src/main/resourcesin a Maven project. This file has one line which is the below:
在 Maven 项目的类路径src/test/resources或src/main/resources的根目录创建一个名为log4jdbc.log4j2.properties的文件。该文件有一行,如下所示:
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
4 Change your DB driver class and URL as below:
4 更改您的数据库驱动程序类和 URL,如下所示:
spring.database.driverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
#append log4jdbc after jdbc part of the URL: hsql example
spring.datasource.url=jdbc:log4jdbc:hsqldb:mem:db_name
In addition to logging SQL it will also log, in tabular format, the results of all queries executed. This can be disabled as per the comment in the sample logging config.
除了记录 SQL 之外,它还将以表格格式记录所有执行查询的结果。这可以根据示例日志配置中的注释禁用。
Sample Output:
示例输出:
10:44:29.400 [main] DEBUG jdbc.sqlonly -
5. select memberrole0_.member_id as member_i2_12_0_, memberrole0_.id as id1_12_0_, memberrole0_.id
as id1_12_1_, memberrole0_.member_id as member_i2_12_1_, memberrole0_.role_id as role_id3_12_1_,
role1_.id as id1_17_2_, role1_.name as name2_17_2_ from member_roles memberrole0_ left outer
join roles role1_ on memberrole0_.role_id=role1_.id where memberrole0_.member_id=104
10:44:29.402 [main] INFO jdbc.resultsettable -
|----------|---|---|----------|--------|---|-----|
|member_id |id |id |member_id |role_id |id |name |
|----------|---|---|----------|--------|---|-----|
|----------|---|---|----------|--------|---|-----|
回答by Alic
SLF4J does not replace log4j, they work together. It removes the dependency on log4j from your library/app.
SLF4J 不会取代 log4j,它们一起工作。它从您的库/应用程序中删除了对 log4j 的依赖。
https://softwareengineering.stackexchange.com/questions/108683/slf4j-vs-log4j-which-one-to-prefer
https://softwareengineering.stackexchange.com/questions/108683/slf4j-vs-log4j-which-one-to-prefer
So it is fine to use log4j configurations if you are using slf4j, as long as you are using log4j underneath slf4j.
因此,如果您使用的是 slf4j,那么使用 log4j 配置是没问题的,只要您在 slf4j 下使用 log4j。
Here's a guide I found that talks about using slf4j with log4j logger: http://saltnlight5.blogspot.ca/2013/08/how-to-configure-slf4j-with-different.html
这是我发现的关于将 slf4j 与 log4j 记录器一起使用的指南:http://saltnlight5.blogspot.ca/2013/08/how-to-configure-slf4j-with-different.html
Essentially you just need to create a properties file here: src/main/resources/log4j.properties
and have it configured the same as the one in your link. And as your link says:
本质上,您只需要在此处创建一个属性文件:src/main/resources/log4j.properties
并将其配置为与链接中的相同。正如你的链接所说:
... For that purpose SQL statements are logged at the DEBUG level (FINE in JDK logging) and results at the TRACE level (FINER in JDK logging) ...
... 为此,SQL 语句记录在调试级别(JDK 日志记录中的 FINE)和 TRACE 级别的结果(JDK 日志记录中的 FINER)...
So make sure you have log4j.logger.org.mybatis.example=DEBUG
set in your properties file.
因此,请确保您已log4j.logger.org.mybatis.example=DEBUG
在属性文件中进行设置。