如何使用

本来想使用原始的log4jdbc,但是后来发现maven远程仓库中目前没有,只有log4jdbc-remix,那就选它了。以下为简略步骤: pom.xml文件:

<dependency>
    <groupId>org.lazyluke</groupId>
    <artifactId>log4jdbc-remix</artifactId>
    <version>0.2.7</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.3</version>
</dependency>

log4j.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration PUBLIC "-//LOG4J//DTD LOG4J//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd" >
    <log4j:configuration>
    <appender name="console-log4jdbc" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss} - %c%m%n" />
    </layout>
    </appender>
    <logger name="jdbc.sqltiming" additivity="false">
    <level value="info"/>
    <appender-ref ref="console-log4jdbc"/>
    </logger>
    <!-- log4jdbc option  log the jdbc results as a table -->
    <logger name="jdbc.resultsettable" additivity="false">
    <level value="info" />
    <appender-ref ref="console-log4jdbc" />  
    </logger>
    </log4j:configuration>

然后在spring的context配置文件中加入:

<bean id="dataSourceSpied" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="100" />
<property name="maxIdle" value="30"/>
<property name="maxWait" value="500"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
<bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">   
<constructor-arg ref="dataSourceSpied" />
</bean>

总结

其实是对原始的datasource做了一次代理,这样就能打印sql执行语句和表格现实的sql结果了

##文档信息