日期:2014-05-16  浏览次数:20492 次

Log4Jdbc测试例子
Log4Jdbc测试例子 http://bbs.chinaunix.net/viewthread.php?tid=1316460
log4jdbc日志框架介绍 http://badqiu.iteye.com/blog/743100

log4j-1.2.15.jar,
log4jdbc4-1.2alpha1.jar,
slf4j-api-1.5.5.jar,
slf4j-log4j12-1.5.5.jar,
同时需要数据库相应版本的JDBC驱动程序。


TestLog4Jdbc.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class TestLog4Jdbc {
private static Logger logger = Logger.getLogger("TestLog4Jdbc");
public static void main(String args[]) {
  PropertyConfigurator.configure("E:\\workspace\\log4jdbcTest\\src\\log4j.properties");
  logger.info("Start......");
  String sDbDriver="net.sf.log4jdbc.DriverSpy";
  String sConnstr = "jdbc:log4jdbc:mysql://192.168.1.6:3306/mysql";
  Connection conn = null;
  ResultSet rs = null;
  Statement stmt = null;
  try {
   Class.forName(sDbDriver);
   conn = DriverManager.getConnection(sConnstr, "root", "");
   stmt = conn.createStatement();
   rs = stmt.executeQuery("select * from user");
   while (rs.next()) {
    System.out.println(rs.getString(1));
   }
   rs.close();
  } catch (Exception e) {
   System.err.print(e);
  }finally{
   logger.info("End......");
  }
}
}



log4j.properties
---------------------------------
log4j.logger.jdbc.sqlonly=ON 
log4j.logger.jdbc.sqltiming=ERROR/OFF
log4j.logger.jdbc.audit=ERROR/OFF
log4j.logger.jdbc.resultset=ERROR/OFF
log4j.logger.jdbc.connection=ERROR/OFF
log4j.rootLogger=WARN,CONSOLE,A1
#ERROR,WARN,INFO,DEBUG
#DEBUG,CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE
log4j.addivity.org.apache=true
###################
# Console Appender
###################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=INFO
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=<<< %-5p >>> %d [%c]  %x -  %m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n