log4j2 mybatis 显示 sql 和 结果集

首先说明版本:

log4j 版本是 第2版beta9,引用jar包

log4j-api-2.0-beta9.jar log4j-core-2.0-beta9.jar


mybatis 是 mybatis-3.2.3


log4j2 配置 如下,命名为 log4j2.xml(log4j2只能用xml或者json,其他命名格式在此不在介绍)将其放在classpath下,内容如下:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration status="OFF"> 
  <appenders> 
    <Console name="Console" target="SYSTEM_OUT"> 
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> 
    </Console> 
  </appenders> 
  <loggers> 
    <logger name="log4j.logger.noModule" level="fatal" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.org.mybatis.jpetstore" level="trace" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.com.opensymphony.xwork2" level="debug" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.com.ibatis" level="trace" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.com.ibatis.common.jdbc.SimpleDataSource" level="debug" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.com.ibatis.common.jdbc.ScriptRunner" level="debug" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="debug" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.org.mybatis" level="debug" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.java.sql" level="debug" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.java.sql.Connection" level="debug" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.java.sql.Statement" level="debug" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.java.sql.PreparedStatement" level="debug" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <logger name="log4j.logger.java.sql.ResultSet" level="debug" additivity="false">
      <appender-ref ref="Console"/> 
    </logger>
    <root level="trace"> 
      <appender-ref ref="Console"/> 
    </root> 
  </loggers> 
</configuration>


光有这个配置是不行滴,因为mybatis加载日志组件是有顺序的,他会按

  • SLF4J

  • Apache Commons Logging

  • Log4j 2

  • Log4j

  • JDK logging

的顺序查找。所以,如果在web应用中用了spring之类的框架,他会包含commons-log包,根据优先级,他就不会去用log4j了,所以还得在mybatis配置文件中配置一条语句,如下

<settings>
     <setting name="logImpl" value="LOG4J2"/>
</settings>

然后就可以了

你可能感兴趣的:(sql,mybatis,结果集,log4j2)