log4jdbc

现大家使用的ibatis,hibernate,spring jdbc的sql日志信息,有一点个缺点是占位符与参数是分开打印的,如果想要拷贝sql至PLSQL Developer客户端直接执行,需要自己拼凑sql。而log4jdbc是在jdbc层的一个日志框架,可以将占位符与参数全部合并在一起显示,方便直接拷贝sql在PLSQL Developer等客户端直接执行,加快调试速度。

 

.简单介绍:

1.没有使用log4jdbc前sql显示:

 

Sql代码  
  1. select username,password from bitth_date > ? and age < ? and username = ?  

 

 

 

2.使用log4jdbc后sql显示:

 

Sql代码  
  1. select username,password from bitth_date > to_date(‘2010-11-11’,’yyyy-mm-dd’) and age < 20 and username = ‘qq2008’ {executed in 2 msec}  

 

 

 

        

这样就可以直接拷贝上面的sql在PLSQL直接执行. 最后的 {executed in 2 msec} 为SQL执行时间.而如果mysql,日志信息将不会出现 to_date()

 

 

.log4jdbc使用:

 

1.spring xml配置(拦截需要处理的dataSource连接)

 

 

 

 

 

Xml代码  
  1. <bean id="log4jdbcInterceptor" class="net.sf.log4jdbc.DataSourceSpyInterceptor" />  
  2.   
  3. <bean id="dataSourceLog4jdbcAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  4.    <property name="interceptorNames">  
  5.        <list>  
  6.           <value>log4jdbcInterceptor</value>          
  7.        </list>  
  8.    </property>  
  9.    <property name="beanNames">  
  10.        <list>  
  11.           <value>dataSource</value>  
  12.        </list>  
  13.    </property>  
  14. </bean>  

  

 

 

2.log4j.properties配置:

 

Java代码  
  1. log4j.logger.jdbc.sqlonly=OFF  
  2. log4j.logger.jdbc.sqltiming=INFO  
  3. log4j.logger.jdbc.audit=OFF  
  4. log4j.logger.jdbc.resultset=OFF  
  5. log4j.logger.jdbc.connection=OFF  

 

 

(日志信息如果全部为off,log4jdbc将不会生效,因此对性能没有任何影响)

 

3.下载slf4j

    log4jdbc需要依赖slf4j日志框架. http://www.slf4j.org/

 

三.扩展说明

 

DataSourceSpyInterceptor为我自己扩展的一个拦截器类,扩展主要是使用AOP的方式,因为log4jdbc原来的方式不适合我的项目.具体源码为:

 

Java代码  
  1. import org.aopalliance.intercept.MethodInterceptor;  
  2. import org.aopalliance.intercept.MethodInvocation;  
  3.   
  4. public class DataSourceSpyInterceptor implements MethodInterceptor {  
  5.   
  6.     private RdbmsSpecifics rdbmsSpecifics = null;  
  7.       
  8.     private RdbmsSpecifics getRdbmsSpecifics(Connection conn) {  
  9.         if(rdbmsSpecifics == null) {  
  10.             rdbmsSpecifics = DriverSpy.getRdbmsSpecifics(conn);  
  11.         }  
  12.         return rdbmsSpecifics;  
  13.     }  
  14.       
  15.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  16.         Object result = invocation.proceed();  
  17.         if(SpyLogFactory.getSpyLogDelegator().isJdbcLoggingEnabled()) {  
  18.             if(result instanceof Connection) {  
  19.                 Connection conn = (Connection)result;  
  20.                 return new ConnectionSpy(conn,getRdbmsSpecifics(conn));  
  21.             }  
  22.         }  
  23.         return result;  
  24.     }  
  25.   
  26. }  

   

 

四.相关下载:

 jar :slf4j-log4j12-1.7.1.jar slf4j-api-1.7.1.jar log4jdbc3-1.2.1.jar log4jdbc-remix-0.2.7.jar

然后在下载 log4jdbc-remix-0.2.7.jar源码

借用hibernate sql格式化工具类,把sql格式化一把。这样可以把hibernate.show_sql设置为false。 
修改Slf4jSpyLogDelegator类 
out.append(sql); 
out.append(" {executed in "); 
out.append(execTime); 
out.append(" msec}"); 
BasicFormatterImpl format = new BasicFormatterImpl(); 
return format.format(out.toString());

log4jdbc:   http://code.google.com/p/log4jdbc/

另外一个对log4jdbc的扩展:  http://code.google.com/p/log4jdbc-remix/

posted @ 2012-11-15 15:25  eggbucket  阅读(486)  评论(0编辑  收藏  举报