- log4j.rootLogger=DEBUG, stdout
-
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%n
-
- #log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
- #log4j.appender.R.File=../logs/service.log
- #log4j.appender.R.layout=org.apache.log4j.PatternLayout
- #log4j.appender.R.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%n
-
- #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
- log4j.logger.java.sql.ResultSet =debug
在開發(fā)的過程中為了能夠顯示sql語句以此來跟蹤mybatis的的操作,雖然網(wǎng)絡(luò)上的配置都是輕輕的一句話概括之,但是按照他們的方法一一試之,都是毫無效果,各種煎熬相比遇到相似問題的同仁應(yīng)該有此感受。為了讓大家能夠少走彎路,現(xiàn)將自己的心得分享給大家。歡迎交流
我們現(xiàn)在分幾種情況處理:
1、此種情況只出現(xiàn)mybatis使用,無其他的包介入(如spring等,因?yàn)橛行┌鼤肫渌娜罩鞠到y(tǒng),會造成干擾,稍后講解)
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
-
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.7.21</version>
- </dependency>
-
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.21</version>
- </dependency>
-
- 1、單獨(dú)開放第三個(gè)無法打印sql語句
- 2、第一種和第三種無法打印sql語句
- 3、其他情況都可以打印出sql語句
- 4、最后得出的結(jié)論可能是版本沖突的問題
- 總而言之
- 1、沒有slf4j,只要log4j即可
- 2、有slf4j,則必須要有slf4j-log4j12,其他的可有可無
此時(shí)可以配置log4j.properties和log4j.xml均可以實(shí)現(xiàn)
如:log4j.properties
- log4j.rootLogger=DEBUG, stdout
-
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%n
-
- #log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
- #log4j.appender.R.File=../logs/service.log
- #log4j.appender.R.layout=org.apache.log4j.PatternLayout
- #log4j.appender.R.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%n
-
- #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
- log4j.logger.java.sql.ResultSet =debug
2、mybatis和其他架構(gòu)包混用,
最頭痛的就是這種,試了很多種辦法,最后才校驗(yàn)出一種合適的辦法。就是一定要用log4j.xml配置
配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration xmlns:log4j="http://jakarta./log4j/"
- debug="false">
-
- <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
- </layout>
- </appender>
-
- <!-- <appender name="FILE" class="org.apache.log4j.RollingFileAppender"> -->
- <!-- <param name="file" value="${user.home}/foss-framework.log" /> -->
- <!-- <param name="append" value="true" /> -->
- <!-- <param name="maxFileSize" value="10MB" /> -->
- <!-- <param name="maxBackupIndex" value="100" /> -->
- <!-- <layout class="org.apache.log4j.PatternLayout"> -->
- <!-- <param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n" /> -->
- <!-- </layout> -->
- <!-- </appender> -->
-
- <!-- <appender name="framework" -->
- <!-- class="com.deppon.foss.framework.server.components.logger.BufferedAppender"> -->
- <!-- <layout class="org.apache.log4j.PatternLayout"> -->
- <!-- <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" /> -->
- <!-- </layout> -->
- <!-- </appender> -->
-
- <!-- 下面是打印 mybatis語句的配置 -->
- <logger name="com.ibatis" additivity="true">
- <level value="DEBUG" />
- </logger>
-
- <logger name="java.sql.Connection" additivity="true">
- <level value="DEBUG" />
- </logger>
-
- <logger name="java.sql.Statement" additivity="true">
- <level value="DEBUG" />
- </logger>
-
- <logger name="java.sql.PreparedStatement" additivity="true">
- <level value="DEBUG" />
- </logger>
-
- <logger name="java.sql.ResultSet" additivity="true">
- <level value="DEBUG" />
- </logger>
-
- <root>
- <level value="DEBUG" />
- <appender-ref ref="CONSOLE" />
- <!-- <appender-ref ref="FILE" /> -->
- <!-- <appender-ref ref="framework" /> -->
- </root>
- </log4j:configuration>
好的能夠打印出sql語句啦,成功啦!
- [24/07/16 03:49:25:025 CST] DEBUG spring.SqlSessionUtils: Creating a new SqlSession
- [24/07/16 03:49:25:025 CST] DEBUG spring.SqlSessionUtils: SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5a90d465] was not registered for synchronization because synchronization is not active
- [24/07/16 03:49:25:025 CST] DEBUG datasource.DataSourceUtils: Fetching JDBC Connection from DataSource
- [24/07/16 03:49:25:025 CST] DEBUG datasource.DriverManagerDataSource: Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3307/security_db?useUnicode=true&characterEncoding=utf-8]
- [24/07/16 03:49:25:025 CST] DEBUG transaction.SpringManagedTransaction: JDBC Connection [com.mysql.jdbc.JDBC4Connection@51e19e59] will not be managed by Spring
- [24/07/16 03:49:25:025 CST] DEBUG UserMapper.getUserNum: ==> Preparing: select COUNT(DISTINCT(user.id)) as total_num from user, user_roles where user.id = user_roles.User_id
- [24/07/16 03:49:25:025 CST] DEBUG UserMapper.getUserNum: ==> Parameters:
- [24/07/16 03:49:25:025 CST] DEBUG UserMapper.getUserNum: <== Total: 1
補(bǔ)充:有興趣的同學(xué)可以試驗(yàn)下
自定義MyBatis 日志
MyBatis使用其內(nèi)部LoggerFactory作為真正的日志類庫使用的門面。其內(nèi)部的LaggerFactory會將日志記錄任務(wù)委托給如下的所示某一個(gè)日志實(shí)現(xiàn),日志記錄優(yōu)先級由上到下順序遞減:
- l SLF4J
- l ApacheCommons Logging
- l Log4j2
- l Log4j
- l JDKlogging
如果MyBatis未發(fā)現(xiàn)上述日志記錄實(shí)現(xiàn),則MyBatis的日志記錄功能無效。
如果你的運(yùn)行環(huán)境中,在classpath中有多個(gè)可用的日志類庫,并且你希望MyBaits使用某個(gè)特定的日志實(shí)現(xiàn),你可以通過調(diào)用以下其中一個(gè)方法:
- ?org.apache.ibatis.logging.LogFactory.useSlf4jLogging();
- ? org.apache.ibatis.logging.LogFactory.useLog4JLogging();
- ? org.apache.ibatis.logging.LogFactory.useLog4J2Logging();
- ? org.apache.ibatis.logging.LogFactory.useJdkLogging();
- ? org.apache.ibatis.logging.LogFactory.useCommonsLogging();
- ? org.apache.ibatis.logging.LogFactory.useStdOutLogging();
|