官方说明文档中讲述要更换jdbc驱动driver和链接的url,例如:
String jdbcdriver = net.sf.log4jdbc.DriverSpy
String jdbcurl = jdbc:log4jdbc:derby://localhost:1527//db-derby-10.2.2.0-bin/databases/MyDatabase
-------------------------------------------------------------------------------------
上述是官方要求的方式,下面笔者介绍一下在SpringMvc+MyBatis3.2.1中配置log4jdbc的代码:
首先需要几个关键jar包:
log4j-1.2.17.jar, log4jdbc4-1.2.jar, mybatis-3.2.1.jar, mybatis-spring-1.2.0.jar, slf4j-api-1.7.6.jar, slf4j-log4j12-1.7.6.jar
下面是关键性代码:
DataSourceSpyInterceptor.java代码如下:
package com.shihuan.interceptor; import java.sql.Connection; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import net.sf.log4jdbc.ConnectionSpy; import net.sf.log4jdbc.RdbmsSpecifics; import net.sf.log4jdbc.DriverSpy; import net.sf.log4jdbc.SpyLogFactory; public class DataSourceSpyInterceptor implements MethodInterceptor { private RdbmsSpecifics rdbmsSpecifics = null; private RdbmsSpecifics getRdbmsSpecifics(Connection conn) { if (rdbmsSpecifics == null) { rdbmsSpecifics = DriverSpy.getRdbmsSpecifics(conn); } 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)); } } return result; } }
引用
上述java代码中import net.sf.log4jdbc.RdbmsSpecifics;和
import net.sf.log4jdbc.DriverSpy;两句正常是不能被引用的,因为从官方下载的log4jdbc4-1.2.jar中RdbmsSpecifics和DriverSpy的级别不是public的,所以在外部的类中无法被引用,笔者这里是下载了官方的源代码并做了少许的更改,就是在方法前加了public,这样改动后就可以进行import了。
在applicationContext.xml文件中加入如下代码:
log4jdbcInterceptor
dataSource
在web.xml中要有如下代码(Log4jConfigListener要在ContextLoaderListener之前加载):
log4jConfigLocation classpath:log4j.properties org.springframework.web.util.Log4jConfigListener contextConfigLocation classpath:applicationContext.xml,classpath:spring-mybatis.xml org.springframework.web.context.ContextLoaderListener
log4j.properties文件代码如下:
log4j.rootLogger=info,console,file log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%-p [%t]: (%F:%L) - %m%n log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=E:/logs/mybatisserver/log4j.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d\: at %l%n %-p - %m%n #log4j.logger.com.ibatis=debug #log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug #log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug #log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug #log4j.logger.java.sql.Connection=debug #log4j.logger.java.sql.Statement=debug #log4j.logger.java.sql.PreparedStatement=debug 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
引用
上述做法要依赖Spring的Aop,注意这种配置不用改变原始的jdbc中url链接字符串了。
笔者最后在控制台打印的结果如下:
INFO [http-bio-8080-exec-1]: (Slf4jSpyLogDelegator.java:370) - select ID,STOCK, VALUE from stocks where ID = 9 {executed in 0 msec} INFO [http-bio-8080-exec-1]: (Slf4jSpyLogDelegator.java:370) - select ID, STOCK, VALUE from stocks {executed in 0 msec} INFO [http-bio-8080-exec-1]: (Slf4jSpyLogDelegator.java:370) - insert into stocks (STOCK, VALUE) values ('我的中国2125950289', '20140501') {executed in 0 msec} INFO [http-bio-8080-exec-1]: (Slf4jSpyLogDelegator.java:370) - update stocks SET STOCK = 'jzg2125950289', VALUE = '0.7295182' where ID = 9 {executed in 0 msec} INFO [http-bio-8080-exec-1]: (Slf4jSpyLogDelegator.java:370) - delete from stocks where ID = 14 {executed in 0 msec} 五月 01, 2014 4:36:52 下午 org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. WARN [http-bio-8080-exec-1]: (MyInterceptor.java:71) - 调用方法:com.shihuan.controller.StockController.indexStockBean(); 花费时间:3094 ms.返回网页:index.jsp