43e8280d6102875eee5215ba3a0391b4531d86bf.svn-base 2.1 KB
package com.espeed.log;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.espeed.tool.ConfigPath;

/**
 *日志工具类
 */
public class LogClass {

	private final static Logger logger = Logger.getLogger("syslog");
	
	/**保存日志*/
//	public static void saveLog(String domain,String loginid,String context){
//		try {
//			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//			String savepath=ConfigPath.getLogPath();
//			System.setProperty("logPath",savepath+"yxyexpLog\\"+domain+"\\"+loginid+"\\"+dateFormat.format(new Date()));
//		     
//			//项目路径
//			String pathew="";
//			pathew = new LogClass().getClass().getResource("/").getPath();
//			pathew = pathew.replace("/classes", "");
//			PropertyConfigurator.configure(pathew+"log4j.properties");// 读取log4j配置文件
//			logger.info(context);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	
	/**错误日志*/
	//public static void errolog(String context,int uid){
	public static void errolog(String context,String account){	
		FileWriter w =null;
		try {
			//以当前时间为日志文件名
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String nowDate=sdf.format(new Date());
			//发生的时间
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String nowDates=sdf1.format(new Date());
			//组建路径
			//String savepath="D:\\yxyweberro\\"+uid;
			String savepath="D:\\yxyweberro\\"+account;
			File files = new File(savepath);
			if (!files.exists()){
				files.mkdirs();
			}
			//写入文件
			w = new FileWriter(savepath+"/"+nowDate+".txt", true);
			w.write(nowDates+":   "+context+"\r\n");
			w.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("日志记录异常");
		}finally{
			if (w != null) {
                try {
                    w.close();
                } catch (Exception e2) {
                   e2.printStackTrace();
                }
            }
		}
	}
	
}