静态实例BusiLog类
import org.apache.log4j.Logger;
import com.bean.LogInfoBean;
import com.common.jdbc.interfaces.JdbcOperation;
import com.common.web.HintInfoHunterThread;
import com.manager.LogManager;
@SuppressWarnings("static-access")
public class BusiLog {
//正常日志标识
public static final String SUCCESS = "0";
//异常常日志标识
public static final String EXCEPTION = "1";
// 单实例
private static BusiLog busiLog;
// 数据库操作对象
private static JdbcOperation jdbcOperation;
//构造方法
private BusiLog()
{
busiLog = this; busiLog.logManager = this.logManager;
}
// 持久化
private static LogManager logManager;
/***
* 日志对象
*/
private static final Logger LOGGER = Logger.getLogger(BusiLog.class);
public void busiLogInit() {
}
public static void log(LogInfoBean logInfoBean ){
try{
//取得单例线程池
ThreadPool threadPool = ThreadPool.getInstance();
//创建一个新的线程实例
HintInfoHunterThread hunterThread = new HintInfoHunterThread(logInfoBean,logManager);
//用线程池中的线程运行线程实例
threadPool.executorService.execute(hunterThread);
}catch(Exception e){
LOGGER.error(e.getMessage());
}
}
/**
* 返回异常对象Throwable详细信息
* @param e Throwable
*/
public static String getExceptionInfo(Throwable e)
{
StringBuffer excepMesSb=new StringBuffer();
excepMesSb.append(e.toString());
StackTraceElement [] messages=e.getStackTrace();
for(int i=0;i<messages.length;i++){
excepMesSb.append("\n"+messages[i].toString());
}
Exception causeE=(Exception)e.getCause();
if(null!=causeE){
excepMesSb.append("\n"+causeE.toString());
StackTraceElement [] causeEMes=causeE.getStackTrace();
for(int i=0;i<causeEMes.length;i++){
excepMesSb.append("\n"+causeEMes[i].toString());
}
}
return excepMesSb.toString();
}
public void setLogManager(LogManager logManager) {
this.logManager = logManager;
}
public void setJdbcOperation(JdbcOperation jdbcOperation)
{
this.jdbcOperation = jdbcOperation;
}
}
线程池单例
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* 单例创建线程池
*
* @author zKF44102
* @version 2012-1-16
* @see ThreadPool
* @since
*/
public class ThreadPool
{
private static ThreadPool tdl = null;
public ExecutorService executorService = null;
private ThreadPool(){
executorService= Executors.newFixedThreadPool(20);
}
public static synchronized ThreadPool getInstance(){
if(tdl == null){
tdl = new ThreadPool();
}
return tdl;
}
}
BEAN对象LogInfoBean
数据库JDBC连接池JdbcOperation
数据入库线程对象HintInfoHunterThread
import java.util.Date;
import org.apache.log4j.Logger;
import com.bean.LogInfoBean;
import com.manager.LogManager;
/**
* 用于记录日志的新线程
*/
public class HintInfoHunterThread extends Thread
{
private LogInfoBean logBeans;
private LogManager logManager;
private final Logger logger = Logger.getLogger(getClass());
public HintInfoHunterThread(LogInfoBean logBean,LogManager logManage)
{
super.setName("loginfo-insert-"+(new Date()).getTime());
this.logBeans = logBean;
this.logManager = logManage;
}
public void run ()
{
logger.info("--------------**ThreadName:"+Thread.currentThread().getName()+"---------[start]----------------------------");
if (null != logBeans){
try{
logManager.addLog(logBeans);
}catch (Exception e){
e.printStackTrace();
}
}
logger.info("--------------**ThreadName:"+Thread.currentThread().getName()+"---------[end]----------------------------");
}
}