package nc.log.arap.bill;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import nc.bs.framework.server.BusinessAppServer;
import nc.vo.pub.lang.UFDate;
/**
 * @Description: 独立日志文件
 * 
 * @author 谢文如 【碧桂园物业NCC】
 * @date 2020-11-16
 * @version with NCC V2005
 */
public class WYNCCARAPLogger {
	
	public static void log(String code,String name,Throwable t,String msg) {
		msg = msg + "\r\n" + getStackTrace(t);
		log(code,name,msg);
	}
	
	private static void log(String code,String name,String msg) {
		FileWriter writer = null;
		try {
			Date currDate = new UFDate().toDate();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String logFileName = "WY_NCC_ARAP_" +code+"_"+ sdf.format(currDate) + ".log";
			String filepath = BusinessAppServer.getInstance().getServerBase() + "/nclogs/"
					+ BusinessAppServer.getInstance().getServer().getServerName() + "/" + logFileName;
			// 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
			writer = new FileWriter(filepath, true);
			SimpleDateFormat formater = new SimpleDateFormat("HH:mm:ss");
			Date date = new Date();
			String time = formater.format(date);
			writer.write("\r\n===【WY_NCC_ARAP】begin===\r\n"+time + ":\r\n" + name + " 异常：\r\n" +msg + "\r\n===【WY_NCC_ARAP】end===");
		} catch (Exception e) {
			nc.bs.logging.Logger.error(e.getMessage(), e);
		}finally{
			if(writer!=null){
				try {
					writer.close();
				} catch (Exception e) {
					e.printStackTrace();
					nc.bs.logging.Logger.error(e.getMessage(), e);
				}
			}
		}
	}
	
	/**
	 * 获取异常的堆栈信息
	 * @param t
	 * @return
	 */
	public static String getStackTrace(Throwable t) {
		if(t==null){
			return "";
		}
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		try {
			t.printStackTrace(pw);
			return sw.toString();
		} finally {
			pw.close();
		}
	}
}
