/*==============================================================================*
 * 用于记录日志文件。                                        					*
 *==============================================================================*/
package  com.Quincy.common.pools;

import java.util.Date;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.BufferedOutputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.text.SimpleDateFormat;

public class dbLogging 
{
	private String logFilePath="";
	
	public static void newFile(String fileName) throws IOException
	{
		File myFile=new File(fileName.toString());
		if(!myFile.exists()) myFile.createNewFile();
	}
	
	public void setLogFilePath(String fileName)
	{
		this.logFilePath=fileName;
	}	
	
	public void log(String logText)
	{
    	try
    	{
    		SimpleDateFormat formater = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
    		String logDate =formater.format(new Date());
      		String dirName = "";
      		String tmpFileName =this.logFilePath;
      		while(tmpFileName.indexOf("/")>0)
      		{
        		dirName = tmpFileName.substring(0,tmpFileName.indexOf("/")+1);
        		tmpFileName = tmpFileName.substring(tmpFileName.indexOf("/")+1);
      		}
      		File tmpFile = new File(dirName);
      		if(!tmpFile.exists())
      		{
        		tmpFile.mkdir();
      		}
      		newFile(tmpFileName);
      		FileOutputStream fp = new FileOutputStream(this.logFilePath,true);
      		logText=logDate+":"+logText+"\r\n";
      		fp.write(logText.getBytes("GBK"));
      		fp.flush();
      		fp.close();
      		File newFile = new File(this.logFilePath);
      		
    	}
    	catch(Exception e){}
  	}
 		
	/*==============================================================================
	 作用：将文本信息写入日志文件 
	 参数：无
	 返回：无
	==============================================================================*/ 
	public void Logging(String msg) 
	{ 
  		log(msg); 
	} 

	/*==============================================================================
	 作用：将文本信息与异常写入日志文件 
	 参数：无
	 返回：无
	===============================================================================*/ 
	public void Logging(Throwable e, String msg) 
	{ 
  		log(msg+":"+e.getMessage()); 
	} 

}	