log4jdbc使用

一、项目介绍

 

log4jdbc项目地址:http://code.google.com/p/log4jdbc/

 

可以将原先的参数占位符“?”,替换为实际参数值,方便调试

如:select username,password from bitth_date > ? and age < ?

替换为:select username,password from bitth_date > to_date(‘2010-11-11’,’yyyy-mm-dd’) and age < 20

另外可以输出调用时间等。

 

二、log4jdbc使用

1. 选择log4jdbc版本,按照网站描述

log4jdbc3-1.2beta2.jar    for JDBC 3 support in JDK 1.4 , JDK 1.5
log4jdbc4-1.2beta2.jar    for JDBC 4 support in JDK 1.6 , JDK 1.7

 

2. 需要使用SLF4j

 

3. 设置jdbc driver。

如:在jdbc.properties中将原先的

jdbc.driver=oracle.jdbc.driver.OracleDriver

修改为

jdbc.driver=net.sf.log4jdbc.DriverSpy

 

4. 修改jdbc url。

如:在jdbc.properties中将原先的

jdbc.url=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=test)))

 

修改为

jdbc.url=jdbc:log4jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=test)))

 

5. 设置logger

如在控制台仅需要输出sql语句的log4j.properties

log4j.logger.jdbc.sqlonly=DEBUG,sql log4j.additivity.jdbc.sqlonly=true log4j.appender.sql=org.apache.log4j.ConsoleAppender log4j.appender.sqlThreshold=debug log4j.appender.sqlTarget=System.out log4j.appender.sqlEncoding=GBK log4j.appender.sql.layout=org.apache.log4j.PatternLayout log4j.appender.sql.layout.ConversionPattern=-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n

 

目前版本为1.2beta2。

 

log4jdbc文档中的log4j.properties示例

!============================================================================== ! 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

你可能感兴趣的:(jdk,sql,jdbc,logging,layer,debugging)