相见恨晚啊,log4jdbc
参考了http://badqiu.iteye.com/blog/743100
maven 依赖
<dependency> <groupId>com.googlecode.log4jdbc</groupId> <artifactId>log4jdbc</artifactId> <version>1.2</version> </dependency>
封装一下下,package名称不能改
package net.sf.log4jdbc;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import java.sql.Connection;
public class DataSourceSpyInterceptor implements MethodInterceptor {
private RdbmsSpecifics rdbmsSpecifics = null;
private RdbmsSpecifics getRdbmsSpecifics(Connection conn) {
if (rdbmsSpecifics == null) {
rdbmsSpecifics = DriverSpy.getRdbmsSpecifics(conn);
}
return rdbmsSpecifics;
}
public Object invoke(MethodInvocation invocation) throws Throwable {
Object result = invocation.proceed();
if (SpyLogFactory.getSpyLogDelegator().isJdbcLoggingEnabled()) {
if (result instanceof Connection) {
Connection conn = (Connection) result;
return new ConnectionSpy(conn, getRdbmsSpecifics(conn));
}
}
return result;
}
}
在spring的datasource配置后面增加:
<bean id="log4jdbcInterceptor" class="net.sf.log4jdbc.DataSourceSpyInterceptor"/> <bean id="dataSourceLog4jdbcAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <value>log4jdbcInterceptor</value> </list> </property> <property name="beanNames"> <list> <value>dataSource</value> </list> </property> </bean>
log4j.properties里面
log4j.logger.jdbc.sqlonly=OFF
log4j.logger.jdbc.sqltiming=INFO
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.connection=OFF