给log4j配上proxool数据库连接池

连接池使用proxool。 

一、先写个辅助类 

package com.wallimn.util;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;

/**
 * 
 * 数据库连接管理类<br/>
 * 编码:wallimn 时间:2009-1-5 下午08:10:52<br/>
 * 版本:V1.0<br/>
 */
public class DbManager {

	private static DbManager manager = new DbManager();

	static{
		URL url = ClassLoader.getSystemResource("proxool.properties");
		try {
			String fileName = url.getPath();
			PropertyConfigurator.configure(fileName);
			System.out.println("连接配置文件:"+fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	public static DbManager getInstance() {
		return manager;
	}

	public Connection getConnection() {
		Connection connection = null;
		try {
			Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
			connection = DriverManager.getConnection("proxool.proxool-pool");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}
}
二、自已定义一JDBCAppender  
package com.wallimn.util;

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.log4j.jdbc.JDBCAppender;

public class JDBCPoolAppender extends JDBCAppender {

    public JDBCPoolAppender() {  
        super();  
    }  
	@Override
	protected Connection getConnection() throws SQLException {
		return DbManager.getInstance().getConnection();
	}

}
三、LOG4J配置文件(log4j.properties)  
log4j.rootLogger=ERROR,stdout 
log4j.logger.SYSTEM = INFO,JDBC 

#stdout configure 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.Threshold=debug 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern= %d %5p [%c] - %m%n 

#jdbc 
log4j.appender.JDBC=com.wallimn.util.JDBCPoolAppender 
log4j.appender.JDBC.threshold=ERROR 
#注,这句不换行。 
log4j.appender.JDBC.sql=INSERT INTO XT_SYS_LOG(USERID,LOGTIME,LOGLEVEL,LOGACTION,LOGMESSAGE) 
VALUES(%X{userId},sysdate,'%p','%l','%m') 
#建表SQL 
############################################################ 
#create table xt_sys_log(userid integer, 
#logtime date default sysdate, 
#loglevel varchar2(5), 
#logaction varchar2(255), 
#logmessage varchar2(2000)); 
############################################################

四、proxool配置文件(proxool.properties) 
jdbc-0.proxool.alias=proxool-pool 
jdbc-0.proxool.driver-url=jdbc:oracle:thin:@127.0.0.1 :1521:swdb 
jdbc-0.proxool.driver-class=oracle.jdbc.driver.OracleDriver 
jdbc-0.user=scott 
jdbc-0.password=tiger 
jdbc-0.proxool.house-keeping-sleep-time=40000 
jdbc-0.proxool.house-keeping-test-sql=select 1 from dual 
jdbc-0.proxool.minimum-connection-count=10 
jdbc-0.proxool.maximum-connection-count=100 
jdbc-0.proxool.maximum-connection-lifetime=600000 
jdbc-0.proxool.maximum-active-time=600000 
jdbc-0.proxool.simultaneous-build-throttle=20 
jdbc-0.proxool.recently-started-threshold=40000 
jdbc-0.proxool.overload-without-refusal-lifetime=60000 
jdbc-0.proxool.verbose=true 
jdbc-0.proxool.trace=true 
jdbc-0.proxool.fatal-sql-exception=Fatalerror 
jdbc-0.proxool.prototype-count=2 

package com.wallimn.test;

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import junit.framework.TestCase;

public class Log4JTest extends TestCase {

	public void testLog1() {
		Logger logger = Logger.getLogger("SYSTEM");
		// System.out.println(Log4JTest.class);
		System.out.println("继承:" + logger.getAdditivity());
		System.out.println("级别:" + logger.getLevel());
		System.out.println("有效的级别:" + logger.getEffectiveLevel());
		MDC.put("userId", "11");
		logger.fatal("test_fatal");
		logger.error("test_Error");
		logger.warn("test_warn");
		logger.info("test_Info");
		logger.debug("test_debug");
		MDC.remove("userId");
	}
}
用到的jar包: 
ojdbc14.jar              
log4j-1.2.16.jar         
proxool-0.9.1.jar        
proxool-cglib.jar        
commons-logging.jar      
junit.jar 

你可能感兴趣的:(log4j)