用法简单总结:
>到 http://code.google.com/p/log4jdbc/ 下载,log4jdbc4-1.2.jar(jdk1.6/1.7) 或log4jdbc3-1.2.jar(jdk1.4/1.5)
>加入SLF4J
>将 net.sf.log4jdbc.DriverSpy 代替你自己的驱动类配置(在application.properties中配置换掉即可)
>将你的jdbc.url加上"jdbc:log4"前缀,
如:jdbc:mysql://192.168.6.250:3306/SHAM
改为:
jdbc:log4jdbc:mysql://192.168.6.250:3306/SHAM
即可
开启log4jdbc日志记录级别
#用于 只记录SQL
log4j.logger.jdbc.sqlonly=DEBUG
#用于记录SQL的执行时间
log4j.logger.jdbc.sqltiming=DEBUG
#用于记录除ResultSets外的所有记录(除非遇到特殊问题,一般不用开启,因打印出的日志很多)
log4j.logger.jdbc.audit=OFF
#用于记录所有的关于resultset的记录,这个会打印出更多信息
log4j.logger.jdbc.resultset=OFF
#记录数据库连接的开关事件,对发现数据库连接泄露问题很有用
log4j.logger.jdbc.connection=OFF
日志还有其他可选项,参加http://code.google.com/p/log4jdbc/
官方给出的log4j实例:
!============================================================================== ! log4j.properties - An example configuration properties file for log4j. ! ! Logging levels are: ! DEBUG < INFO < WARN < ERROR < FATAL !============================================================================== ! turn on the internal log4j debugging flag so we can see what it is doing log4j.debug=true !============================================================================== ! JDBC API layer call logging : ! INFO shows logging, DEBUG also shows where in code the jdbc calls were made, ! setting DEBUG to true might cause minor slow-down in some environments. ! If you experience too much slowness, use INFO instead. ! Log all JDBC calls except for ResultSet calls log4j.logger.jdbc.audit=INFO,jdbc log4j.additivity.jdbc.audit=false ! Log only JDBC calls to ResultSet objects log4j.logger.jdbc.resultset=INFO,jdbc log4j.additivity.jdbc.resultset=false ! Log only the SQL that is executed. log4j.logger.jdbc.sqlonly=DEBUG,sql log4j.additivity.jdbc.sqlonly=false ! Log timing information about the SQL that is executed. log4j.logger.jdbc.sqltiming=DEBUG,sqltiming log4j.additivity.jdbc.sqltiming=false ! Log connection open/close events and connection number dump log4j.logger.jdbc.connection=FATAL,connection log4j.additivity.jdbc.connection=false ! the appender used for the JDBC API layer call logging above, sql only log4j.appender.sql=org.apache.log4j.FileAppender log4j.appender.sql.File=./logs/sql.log log4j.appender.sql.Append=false log4j.appender.sql.layout=org.apache.log4j.PatternLayout log4j.appender.sql.layout.ConversionPattern=-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n ! the appender used for the JDBC API layer call logging above, sql timing log4j.appender.sqltiming=org.apache.log4j.FileAppender log4j.appender.sqltiming.File=./logs/sqltiming.log log4j.appender.sqltiming.Append=false log4j.appender.sqltiming.layout=org.apache.log4j.PatternLayout log4j.appender.sqltiming.layout.ConversionPattern=-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n ! the appender used for the JDBC API layer call logging above log4j.appender.jdbc=org.apache.log4j.FileAppender log4j.appender.jdbc.File=./logs/jdbc.log log4j.appender.jdbc.Append=false log4j.appender.jdbc.layout=org.apache.log4j.PatternLayout log4j.appender.jdbc.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n ! the appender used for the JDBC Connection open and close events log4j.appender.connection=org.apache.log4j.FileAppender log4j.appender.connection.File=./logs/connection.log log4j.appender.connection.Append=false log4j.appender.connection.layout=org.apache.log4j.PatternLayout log4j.appender.connection.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n
以下转载自:https://code.google.com/p/rapid-framework/wiki/log4jdbc
log4jdbc日志框架介绍
现大家使用的ibatis,hibernate,spring jdbc的sql日志信息,有一点个缺点是占位符与参数是分开打印的,如果想要拷贝sql至PLSQL Developer客户端直接执行,需要自己拼凑sql。而log4jdbc是在jdbc层的一个日志框架,可以将占位符与参数全部合并在一起显示,方便直接拷贝sql在PLSQL Developer等客户端直接执行,加快调试速度。
一.简单介绍
1.没有使用log4jdbc前sql显示:
select username,password from bitth_date >?and age <?and username =?
2.使用log4jdbc后sql显示:
select username,password from bitth_date > to_date(‘2010-11-11’,’yyyy-mm-dd’)and age <20and username =‘qq2008’{executed in2 msec}
这样就可以直接拷贝上面的sql在PLSQL直接执行. 最后的 {executed in 2 msec} 为SQL执行时间.而如果mysql,日志信息将不会出现 to_date()
二.log4jdbc使用
1.spring xml配置(拦截需要处理的dataSource连接)
<beanid="log4jdbcInterceptor"class="net.sf.log4jdbc.DataSourceSpyInterceptor"/> <beanid="dataSourceLog4jdbcAutoProxyCreator"class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <propertyname="interceptorNames"> <list> <value>log4jdbcInterceptor</value> </list> </property> <propertyname="beanNames"> <list> <value>dataSource</value> </list> </property> </bean>
2.log4j.properties配置
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
(日志信息如果全部为off,log4jdbc将不会生效,因此对性能没有任何影响)
3.下载slf4j
log4jdbc需要依赖slf4j日志框架. http://www.slf4j.org/
三.扩展说明
DataSourceSpyInterceptor为我自己扩展的一个拦截器类,扩展主要是使用AOP的方式,因为log4jdbc原来的方式不适合我的项目.具体源码为:
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; publicclassDataSourceSpyInterceptorimplementsMethodInterceptor{ privateRdbmsSpecifics rdbmsSpecifics =null; privateRdbmsSpecifics getRdbmsSpecifics(Connection conn){ if(rdbmsSpecifics ==null){ rdbmsSpecifics =DriverSpy.getRdbmsSpecifics(conn); } return rdbmsSpecifics; } publicObject invoke(MethodInvocation invocation)throwsThrowable{ Object result = invocation.proceed(); if(SpyLogFactory.getSpyLogDelegator().isJdbcLoggingEnabled()){ if(result instanceofConnection){ Connection conn =(Connection)result; returnnewConnectionSpy(conn,getRdbmsSpecifics(conn)); } } return result; } }
四.相关链接
- log4jdbc: http://code.google.com/p/log4jdbc/
- 另外一个对log4jdbc的扩展: http://code.google.com/p/log4jdbc-remix/
常见错误:
Unable to create initial connections of pool.
java.sql.SQLException: Driver:net.sf.log4jdbc.DriverSpy@5b74e91b returned null for URL:jdbc:log4jdbc:mysql://172.28.5.92/FunPlugin?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&allowMultiQueries=true&autoReconnect=true
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:295)
解决:
1: 你的驱动没有在类路径下
2: jdbc连接URL配错了
3: 这个问题反映的就是通过以上配置没有连接到DB