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/
- 引入项目依赖
12345<
dependency
>
<
groupId
>org.bgee.log4jdbc-log4j2</
groupId
>
<
artifactId
>log4jdbc-log4j2-jdbc4</
artifactId
>
<
version
>1.16</
version
>
</
dependency
>
-
扩展的一个拦截器类
1234567891011121314151617181920212223242526272829303132333435363738394041public
class
DataSourceSpyInterceptor
implements
MethodInterceptor {
private
RdbmsSpecifics rdbmsSpecifics =
null
;
private
static
Method method =
null
;
private
RdbmsSpecifics 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();
}
}
return
rdbmsSpecifics;
}
@Override
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),SpyLogFactory.getSpyLogDelegator());
}
}
return
result;
}
}
-
配置spring配置文件applicationContext.xml
1234567891011121314<
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
>
其中net.sf.log4jdbc.DataSourceSpyInterceptor为刚刚新建拦截器所在路径
-
配置你的日志配置文件,我的项目用的是logback,在logback.xml中配置
12345<!--log4jdbc -->
<
logger
name
=
"jdbc.sqltiming"
level
=
"DEBUG"
/>
<
logger
name
=
"jdbc.sqlonly"
level
=
"DEBUG"
/>
<
logger
name
=
"jdbc.audit"
level
=
"ERROR"
/>
<
logger
name
=
"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-log4j2</groupId> <artifactId>log4jdbc-log4j2-jdbc3</artifactId> <version>1.16</version> </dependency>
作者:九州浪子
出处:http://www.cnblogs.com/juforg/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。