如何使用 log4j 打印 SQL 查询结果日志?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16235106/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:01:17  来源:igfitidea点击:

How can I print SQL query result log with log4j?

sqlspringjdbclog4jmybatis

提问by firia2000

I'm using Spring 3.1.1, MyBatis 3.1.1, MySQL 5.0.67. My Spring configuration is below:

我使用的是 Spring 3.1.1、MyBatis 3.1.1、MySQL 5.0.67。我的 Spring 配置如下:

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">    
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="validationQuery" value="select 1"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="14400000"/>               
    <property name="testOnBorrow" value="false"/>       
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:mybatis/myBatisConfig.xml"/>
</bean>

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg ref="sqlSessionFactory"/>
</bean>

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    p:dataSource-ref="dataSource"/>

<tx:annotation-driven transaction-manager="transactionManager"/>

And log4.properties is below:

log4.properties 如下:

log4j.logger.org.springframework=DEBUG
log4j.logger.org.apache=DEBUG
log4j.logger.org.mybatis=DEBUG

log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG

With these configuration, I can see SQL query statement which is executed and parameters to that query but I can't see query result log. My log is like this:

通过这些配置,我可以看到执行的 SQL 查询语句和该查询的参数,但我看不到查询结果日志。我的日志是这样的:

[org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
[org.mybatis.spring.SqlSessionUtils] - SqlSession             [org.apache.ibatis.session.defaults.DefaultSqlSession@4ccdd1f] was not registered for synchronization because synchronization is not active
[org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
[org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection     [ProxyConnection[PooledConnection[com.mysql.jdbc.JDBC4Connection@3cfde82]]] will not be managed by Spring
[java.sql.Connection] - ooo Using Connection     [ProxyConnection[PooledConnection[com.mysql.jdbc.JDBC4Connection@3cfde82]]]
[java.sql.Connection] - ==>  Preparing: SELECT col FROM table WHERE col1=? AND col2=? 
[java.sql.PreparedStatement] - ==> Parameters: 93(Integer), 4(Integer)
[org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession     [org.apache.ibatis.session.defaults.DefaultSqlSession@4ccdd1f]
[org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource

Is there any way to print log including query result?

有没有办法打印包括查询结果的日志?

采纳答案by Diego Lopez

Two ways:

两种方式:

  • log4j.logger.java.sql.ResultSet=TRACE
  • log4j.logger.java.sql.ResultSet=TRACE

Or use the namespaces to set logging. This is the only logging method in mybatis 3.2

或者使用命名空间来设置日志记录。这是mybatis 3.2中唯一的日志方式

http://mybatis.github.io/mybatis-3/logging.html

http://mybatis.github.io/mybatis-3/logging.html

回答by raspacorp

What works for me is:

对我有用的是:

log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils=TRACE

log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils=TRACE

This prints entries like these:

这将打印如下条目:

...
TRACE SimpleAsyncTaskExecutor-1 org.springframework.jdbc.core.StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [12345], value class [java.math.BigDecimal], SQL type 3
TRACE SimpleAsyncTaskExecutor-1 org.springframework.jdbc.core.StatementCreatorUtils - Setting SQL statement parameter value: column index 2, parameter value [ADDRESS], value class [java.lang.String], SQL type 12
TRACE SimpleAsyncTaskExecutor-1 org.springframework.jdbc.core.StatementCreatorUtils - Setting SQL statement parameter value: column index 3, parameter value [20130916], value class [java.math.BigDecimal], SQL type 3
...

In my case I am using running a query inside a JdbcBatchItemWriter in a Spring Batch application, which uses named params so it may not be a general solution.

在我的情况下,我在 Spring Batch 应用程序中的 JdbcBatchItemWriter 中使用运行查询,该应用程序使用命名参数,因此它可能不是通用解决方案。