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

配置log4j2.xml
实现用Log4j2来打印Mybatis的SQL很简单,先配置一个name为consolePrint的附加器,指定输出格式 

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

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="1800">
    <appenders>
        <Console name="consolePrint" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </appenders>

    <loggers>
        <!-- 将业务dao接口填写进去,并用控制台输出即可 -->
        <logger name="com.amayadream.freemarker.dao" level="DEBUG" additivity="false">
            <appender-ref ref="consolePrint"/>
        </logger>

        <root level="info">
            <appender-ref ref="consolePrint" />
        </root>
    </loggers>
</Configuration>

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

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 = ? 
23:02:58 [http-apr-8090-exec-17] DEBUG com.amayadream.freemarker.dao.IPhysicalDao.showColumns - ==> Parameters: WEBCHAT_LOG(String)
23:02:58 [http-apr-8090-exec-17] DEBUG com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==      Total: 6

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

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 = ? 
23:00:36 [http-apr-8090-exec-11] DEBUG com.amayadream.freemarker.dao.IPhysicalDao.showColumns - ==> Parameters: WEBCHAT_LOG(String)
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
23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: ID, 日志编号, VARCHAR2, 32, null, null, N
23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: USERID, 用户名, VARCHAR2, 32, null, null, N
23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: TIME, 时间, VARCHAR2, 32, null, null, Y
23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: TYPE, 类型, VARCHAR2, 32, null, null, Y
23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: DETAIL, 详情, VARCHAR2, 300, null, null, Y
23:00:36 [http-apr-8090-exec-11] TRACE com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==        Row: IP, ip地址, VARCHAR2, 32, null, null, Y
23:00:36 [http-apr-8090-exec-11] DEBUG com.amayadream.freemarker.dao.IPhysicalDao.showColumns - <==      Total: 6


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值