使用Log4j2打印Mybatis SQL语句以及结果集

来源:互联网 发布:软件开发供应商 编辑:程序博客网 时间:2024/05/29 18:25
配置log4j2.xml
实现用Log4j2来打印Mybatis的SQL很简单,先配置一个name为consolePrint的附加器,指定输出格式 

然后在loggers下配置一个logger,name指向项目持久层接口的package,也就是和Mybatis配置文件对应的接口包,再定义输出方式就可以了

[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Configuration status="INFO" monitorInterval="1800">  
  3.     <appenders>  
  4.         <Console name="consolePrint" target="SYSTEM_OUT">  
  5.             <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />  
  6.         </Console>  
  7.     </appenders>  
  8.   
  9.     <loggers>  
  10.         <!-- 将业务dao接口填写进去,并用控制台输出即可 -->  
  11.         <logger name="com.amayadream.freemarker.dao" level="DEBUG" additivity="false">  
  12.             <appender-ref ref="consolePrint"/>  
  13.         </logger>  
  14.   
  15.         <root level="info">  
  16.             <appender-ref ref="consolePrint" />  
  17.         </root>  
  18.     </loggers>  
  19. </Configuration>  

这里要注意,如果将level定义为DEBUG则只会打印出SQL语句,SQL参数以及结果条数,例如下面

 

[plain] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. 23:02:58 [http-apr-8090-exec-17] DEBUG com.amayadream.freemarker.dao.IPhysicalDao.showColumns - ==>  Preparing: select t.column_name, nvl(c.comments, t.column_name) as comments, t.data_type, t.data_length, t.data_precision, t.data_scale, nullable from user_tab_columns t left join user_col_comments c on c.table_name = t.table_name and c.column_name = t.column_name where t.table_name = ?   
  2. 23:02:58 [http-apr-8090-exec-17] DEBUG com.amayadream.freemarker.dao.IPhysicalDao.showColumns - ==> Parameters: WEBCHAT_LOG(String)  
  3. 23:02:58 [http-apr-8090-exec-17] DEBUG com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==      Total: 6  

如果将level定义为TRACE则会打印出SQL语句,SQL参数以及结果集还有结果条数,例如下面:

 

[plain] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. 23:00:36 [http-apr-8090-exec-11] DEBUG com.amayadream.freemarker.dao.IPhysicalDao.showColumns - ==>  Preparing: select t.column_name, nvl(c.comments, t.column_name) as comments, t.data_type, t.data_length, t.data_precision, t.data_scale, nullable from user_tab_columns t left join user_col_comments c on c.table_name = t.table_name and c.column_name = t.column_name where t.table_name = ?   
  2. 23:00:36 [http-apr-8090-exec-11] DEBUG com.amayadream.freemarker.dao.IPhysicalDao.showColumns - ==> Parameters: WEBCHAT_LOG(String)  
  3. 23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==    Columns: COLUMN_NAME, COMMENTS, DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, NULLABLE  
  4. 23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: ID, 日志编号, VARCHAR2, 32, null, null, N  
  5. 23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: USERID, 用户名, VARCHAR2, 32, null, null, N  
  6. 23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: TIME, 时间, VARCHAR2, 32, null, null, Y  
  7. 23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: TYPE, 类型, VARCHAR2, 32, null, null, Y  
  8. 23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: DETAIL, 详情, VARCHAR2, 300, null, null, Y  
  9. 23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: IP, ip地址, VARCHAR2, 32, null, null, Y  
  10. 23:00:36 [http-apr-8090-exec-11] DEBUG com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==      Total: 6  

0 0
原创粉丝点击