- 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
在开发的过程中为了能够显示sql语句以此来跟踪mybatis的的操作,虽然网络上的配置都是轻轻的一句话概括之,但是按照他们的方法一一试之,都是毫无效果,各种煎熬相比遇到相似问题的同仁应该有此感受。为了让大家能够少走弯路,现将自己的心得分享给大家。欢迎交流
我们现在分几种情况处理:
1、此种情况只出现mybatis使用,无其他的包介入(如spring等,因为有些包会引入其他的日志系统,会造成干扰,稍后讲解)
- <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、单独开放第三个无法打印sql语句
- 2、第一种和第三种无法打印sql语句
- 3、其他情况都可以打印出sql语句
- 4、最后得出的结论可能是版本冲突的问题
- 总而言之
- 1、没有slf4j,只要log4j即可
- 2、有slf4j,则必须要有slf4j-log4j12,其他的可有可无
此时可以配置log4j.properties和log4j.xml均可以实现
如: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和其他架构包混用,
最头痛的就是这种,试了很多种办法,最后才校验出一种合适的办法。就是一定要用log4j.xml配置
配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration xmlns:log4j="http://jakarta.apache.org/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
补充:有兴趣的同学可以试验下
自定义MyBatis 日志
MyBatis使用其内部LoggerFactory作为真正的日志类库使用的门面。其内部的LaggerFactory会将日志记录任务委托给如下的所示某一个日志实现,日志记录优先级由上到下顺序递减:
- l SLF4J
- l ApacheCommons Logging
- l Log4j2
- l Log4j
- l JDKlogging
如果MyBatis未发现上述日志记录实现,则MyBatis的日志记录功能无效。
如果你的运行环境中,在classpath中有多个可用的日志类库,并且你希望MyBaits使用某个特定的日志实现,你可以通过调用以下其中一个方法:
- ž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();