spring+ibatis log4j.xml 控制台打印sql语句

log4j.xml文件写法

spring+ibatis 输出sql语句 的log4j.xml写法

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/ ">

 <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d [%t] %p - %m%n" />
  </layout>
  <filter class="org.apache.log4j.varia.LevelRangeFilter">
   <param name="LevelMin" value="INFO" />
   <param name="LevelMax" value="FATAL" />
  </filter>
 </appender>

<!-- 正常的日记文件INFO -- WARN --> 

<appender name="Access" class="org.apache.log4j.RollingFileAppender">
  <param name="File" value="G:/log/access.log" />
  <param name="Append" value="false" />
  <param name="MaxFileSize" value="1024" />
  <param name="MaxBackupIndex" value="2" />
  <param name="Threshold" value="INFO" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d [%t] %p - %m%n" />
  </layout>
  <filter class="org.apache.log4j.varia.LevelRangeFilter">
   <param name="LevelMin" value="INFO" />
   <param name="LevelMax" value="WARN" />
   <param name="AcceptOnMatch" value="true" />
  </filter>
 </appender>

<!-- 出错的日记文件ERROR -- FATAL --> 

 <appender name="Error" class="org.apache.log4j.RollingFileAppender">
  <param name="Append" value="false" />
  <param name="File" value="G:/log/error.log" />
  <param name="MaxFileSize" value="1024" />
  <param name="MaxBackupIndex" value="2" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d [%t] %p - %m%n" />
  </layout>
  <filter class="org.apache.log4j.varia.LevelRangeFilter">
   <param name="LevelMin" value="ERROR" />
   <param name="LevelMax" value="ERROR" />
   <param name="AcceptOnMatch" value="true" />
  </filter>
 </appender>


 <root>
  <appender-ref ref="STDOUT" />
  <appender-ref ref="Access" />
  <appender-ref ref="Error" />
 </root>
</log4j:configuration>

=====================================================================================================

增加一些log内容,ibatis输出sql语句的log.

新增一个appender,这里的filter必需要设置成为DEBUG的level. 设置其他的,不会输出sql语句。

 <appender name="IBatis" class="org.apache.log4j.ConsoleAppender">
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%-4r [%t] %-5p %c %x - %m%n" />
  </layout>
  <filter class="org.apache.log4j.varia.LevelRangeFilter">
   <param name="LevelMin" value="DEBUG" />
   <param name="LevelMax" value="DEBUG" />
  </filter>
 </appender>

再增加N个logger.

<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" />
  <appender-ref ref="IBatis" />
 </logger>
 
 <logger name="java.sql.ResultSet" additivity="true">
  <level value="DEBUG" />
  <appender-ref ref="IBatis" />
 </logger>

上面的logger,也一个都不能少。
<appender-ref ref="IBatis" />指向上面的IBatis,就会和log一起输出,没加<appender-ref ref="IBatis" />标签的,日记里不会显示内容。

你可能感兴趣的:(spring)