sql日志框架log4jdbc的AOP式使用
log4jdbc.log4j2
参考:1. http://badqiu.iteye.com/blog/743100
2. https://code.google.com/p/log4jdbc/
3. https://code.google.com/p/log4jdbc-log4j2/
- 引入项目依赖
1 2 3 4 5 <dependency><groupId>org.bgee.log4jdbc-log4j2groupId><artifactId>log4jdbc-log4j2-jdbc4artifactId><version>1.16version>dependency> -
扩展的一个拦截器类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 publicclassDataSourceSpyInterceptorimplementsMethodInterceptor {privateRdbmsSpecifics rdbmsSpecifics =null;privatestaticMethod method =null;privateRdbmsSpecifics getRdbmsSpecifics(Connection conn) {if(rdbmsSpecifics ==null) {try{if(null== method) {method = DriverSpy.class.getDeclaredMethod("getRdbmsSpecifics", Connection.class);}method.setAccessible(true);rdbmsSpecifics = (RdbmsSpecifics) method.invoke(null, conn);method.setAccessible(false);}catch(SecurityException e) {e.printStackTrace();}catch(NoSuchMethodException e) {e.printStackTrace();}catch(IllegalArgumentException e) {e.printStackTrace();}catch(IllegalAccessException e) {e.printStackTrace();}catch(InvocationTargetException e) {e.printStackTrace();}}returnrdbmsSpecifics;}@OverridepublicObject invoke(MethodInvocation invocation)throwsThrowable {Object result = invocation.proceed();if(SpyLogFactory.getSpyLogDelegator().isJdbcLoggingEnabled()) {if(resultinstanceofConnection) {Connection conn = (Connection)result;returnnewConnectionSpy(conn,getRdbmsSpecifics(conn),SpyLogFactory.getSpyLogDelegator());}}returnresult;}} -
配置spring配置文件applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <beanid="log4jdbcInterceptor"class="net.sf.log4jdbc.DataSourceSpyInterceptor"/><beanid="dataSourceLog4jdbcAutoProxyCreator"class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><propertyname="interceptorNames"><list><value>log4jdbcInterceptorvalue>list>property><propertyname="beanNames"><list><value>dataSourcevalue>list>property>bean>其中net.sf.log4jdbc.DataSourceSpyInterceptor为刚刚新建拦截器所在路径
-
配置你的日志配置文件,我的项目用的是logback,在logback.xml中配置
1 2 3 4 5 <loggername="jdbc.sqltiming"level="DEBUG"/><loggername="jdbc.sqlonly"level="DEBUG"/><loggername="jdbc.audit"level="ERROR"/><loggername="jdbc.connection"level="DEBUG"/>配置说明请参考https://code.google.com/p/log4jdbc-log4j2/ 中的说明4.2.2. Configure the loggers
现在你的日志文件中就会有优雅的 SQL 语句打印出来了,对原项目基本没有影响
PS:
遇到这样的exception :
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.AbstractMethodError: oracle.jdbc.driver.OracleResultSetImpl.isClosed()Z
简单点的解决办法是降低版本:
<dependency> <groupId>org.bgee.log4jdbc-log4j2groupId> <artifactId>log4jdbc-log4j2-jdbc3artifactId> <version>1.16version> dependency>