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 语句打印出来了,对原项目基本没有影响