问题描述
Spring3 struts2 mybatis3JAR包:Log4J配置:log4j.rootLogger=info, stdout, Rlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%nlog4j.appender.R=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.R.File=../logs/service.loglog4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%nlog4j.logger.com.ibatis = debuglog4j.logger.com.ibatis.common.jdbc.SimpleDataSource = debuglog4j.logger.com.ibatis.common.jdbc.ScriptRunner = debuglog4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = debuglog4j.logger.java.sql.Connection = debuglog4j.logger.java.sql.Statement = debuglog4j.logger.java.sql.PreparedStatement = debug,Rlog4j.logger.java.sql.ResultSet =debug发现只有当 log4j.rootLogger=debug, stdout, R 时才打印SQL语句和结果出来。如果是info就不打印了。找了很久度娘和谷哥,不知道怎么回事。。
解决方案
我把mybatis3.2的源码下载之后,把里面PooledDataSource类的log输出部分,换成log.warn之后,重新打jar包,放到项目中,日志级别改为info,如:log4j.rootLogger=info, stdout, Rlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%nlog4j.appender.R=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.R.File=../logs/service.loglog4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%nlog4j.logger.com.ibatis = debuglog4j.logger.com.ibatis.common.jdbc.SimpleDataSource = debuglog4j.logger.com.ibatis.common.jdbc.ScriptRunner = debuglog4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = debuglog4j.logger.java.sql.Connection = debuglog4j.logger.java.sql.Statement = debuglog4j.logger.java.sql.PreparedStatement = debuglog4j.logger.java.sql.ResultSet =debug此时,info级别能够输出warn级别的日志,我的日志显示如下:[service] 2013-04-19 10:30:57,982 - org.apache.ibatis.datasource.pooled.PooledDataSource -0 [main] WARN org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.[service] 2013-04-19 10:30:57,985 - org.apache.ibatis.datasource.pooled.PooledDataSource -3 [main] WARN org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.[service] 2013-04-19 10:30:57,986 - org.apache.ibatis.datasource.pooled.PooledDataSource -4 [main] WARN org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.[service] 2013-04-19 10:30:57,996 - org.apache.ibatis.datasource.pooled.PooledDataSource -14 [main] WARN org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.[service] 2013-04-19 10:30:59,025 - org.apache.ibatis.datasource.pooled.PooledDataSource -1043 [main] WARN org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 22323092.[service] 2013-04-19 10:30:59,076 - org.apache.ibatis.datasource.pooled.PooledDataSource -1094 [main] WARN org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 22323092 to pool.很明显,这是显示warn的日志信息了。说明mybatis3只是打印了debug级别的日志。
解决方案二:
最近也在苦恼这个问题。终于找到答案了。自己的项目已经可以打印SQL了mybatis使用的是 slf4j 做的日志。在你的jar文件中添加slf4j-log4j12的jar文件就好了。参见:http://wenku.baidu.com/view/d409554be518964bcf847cf4.html
解决方案三:
你的问题我知道为何了。因为mybatis源码中只输出了debug级别的日志,没有info级别的日志。所以你定义info级别没有输出任何信息。
解决方案四:
除了 og4j.logger.java.sql.Connection = debug,stdout log4j.logger.java.sql.Statement = debug,stdout log4j.logger.java.sql.PreparedStatement = debug,stdout log4j.logger.java.sql.ResultSet =debug,stdout 还有可能是 你缺少slf4j-api-1.6.2.jar slf4j-log4j12-1.6.2.jar这两个jar包,因为mybatis 默认的日志输出 需要这两个jar包
解决方案五:
一行就可以了log4j.logger.java.sql=debug
解决方案六:
log4j.logger.java.sql.Connection = debug,stdout log4j.logger.java.sql.Statement = debug,stdout log4j.logger.java.sql.PreparedStatement = debug,stdout log4j.logger.java.sql.ResultSet =debug,stdout 这是因为你没有指定 Appender 如上即可
解决方案七:
我这里使用你的log4j配置,是可以打印ibatis的sql语句的,跟R没关系,因为你后面的R实际上是做了配置的。
解决方案八:
log4j.logger.java.sql.PreparedStatement = debug,stdoutlog4j.rootLogger=info, stdout,debug试试
解决方案九:
log4j.logger.java.sql.PreparedStatement = debug,R 应该是这行配置的问题,把R去掉试试
解决方案十:
log4j.logger.com.ibatis=DEBUG log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG参见:http://blog.csdn.net/hanzhou4519/article/details/7763412