原创

ibatis配置log4j打印sql日志

1.当前配置

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="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS} [%p] [%t] [%l] %m %n" />
        </layout>
    </appender>

    <appender name="FILE-LOG" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="/var/log/webapps/edm-job_catalina.log" />
        <!-- <param name="DatePattern" value="'.'yyyy-MM-dd" />
        <param name="BufferSize" value="8192"/>
        <param name="ImmediateFlush" value="false"/>
        <param name="BufferedIO" value="true"/> -->
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS} [%p] [%t] [%l] %m %n" />
        </layout>
    </appender>

    <!-- ibatis配置-->

    <root>
        <level value="info" />
         <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE-LOG" />
    </root>

</log4j:configuration>

2.增加ibatis相关配置


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

<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>

3.properties配置

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %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,stdout
正文到此结束
本文目录