package nc.log.uap.wgpt;

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.pubapp.AppContext;
import nccloud.commons.lang.StringUtils;
/**
 * @Description: 日志文件
 * 
 * @author 谢文如 【碧桂园-物业板块-网购平台接口】
 * @date 2019-3-6
 * @version with NC V6.3
 */
public class WuYeWgptLogger {
	
	public static void log(String itfname,String iftCode,String msg) {
		FileWriter writer = null;
		try {
			if(StringUtils.isBlank(iftCode)){
				iftCode = "";
			}
			String serverBase = BusinessAppServer.getInstance().getServerBase();
			String serverName = BusinessAppServer.getInstance().getServer().getServerName();
			Date currDate = AppContext.getInstance().getServerTime().getDate().toDate();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String logFileName = iftCode+"_"+ sdf.format(currDate) + ".log";
			String filepath = serverBase + "/nclogs/" + serverName + "/" + 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("==="+itfname+"==="+time + ":\r\n" + msg + "\r\n");
		} 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) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		try {
			t.printStackTrace(pw);
			return sw.toString();
		} finally {
			pw.close();
		}
	}
	
}
