package com.cslg.ppa.common.core.log;


import com.cslg.ppa.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 日志工具类
 * @author Peach
 *
 */
public class LogHelper {
	
	private static Logger logger = null;

	//------------------------------------------系统日志记录-------------------------
	/**
	 * @param info
	 */
	public static void log(Object...info) {
		logger = LoggerFactory.getLogger("");
		logger.info("info={}",info);
	}
	
	public static void log(Throwable e,Object...info) {
		logger = LoggerFactory.getLogger("");
		logger.info("exception={} info={}",throwable2String(e),info);
	}
	
	/**
	 * @param clazz
	 * @param info
	 */
	public static void log(Class clazz, String info) {
		logger = LoggerFactory.getLogger(clazz);
		logger.info(info);
	}
	
	/**
	 * @param clazz
	 * @param e
	 */
	public static void log(Class clazz, Throwable e) {
		log(clazz,throwable2String(e));
	}
	
	/**
	 * @param name
	 * @param info
	 */
	@Deprecated
	public static void log(String name, String info) {
		logger = LoggerFactory.getLogger(name);
		logger.info(name+"-"+info);
	}
	
	/**
	 * @param name
	 * @param e
	 */
	@Deprecated
	public static void log(String name, Throwable e) {
		log(name,throwable2String(e));
	}
	
	
	/**
	 * @param name
	 * @param info
	 * @param e
	 */
	@Deprecated
	public static void log(String name, String info,Throwable e) {
		log(name,info+"-"+throwable2String(e));
	}
	
	
	/**
	 * @param clazz
	 * @param info
	 * @param e
	 */
	public static void log(Class clazz, String info,Throwable e) {
		log(clazz,info+"-"+throwable2String(e));
	}
	
	/**
	 * 将异常信息转为string
	 * @param e
	 * @return
	 */
	private static String throwable2String(Throwable e){
		if(checkBaseBusinessException(e)) {
			BusinessException bbe = (BusinessException)e;
			return "----->"+bbe.getErrorMessage();
		}
		PrintWriter pw = null;
		StringWriter sw = null;
		try{
			sw = new StringWriter();
			pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			pw.flush();
			sw.flush();
		}catch (Exception e1) {
			logger.info(e.getMessage());
		}finally{
			if(sw!=null){
				try{
					sw.close();
				}catch (Exception e2) {
					e2.printStackTrace();
					return "";
				}
			}else{
				return "";
			}
			if(pw!=null){
				pw.close();
			}
			return sw.toString();
		}
		
	}
	
	
	/**
	* @Title: checkBaseBusinessException
	* @Description: 判断异常是否为系统异常
	* @param @param e
	* @param @return    参数
	* @return boolean    返回类型
	* @throws
	* @author Orange
	* @date 2019年1月23日
	*/
	private static boolean checkBaseBusinessException(Throwable e) {
		return BusinessException.class.isInstance(e);
	}
	
	
	//------------------------------------------多业务日志记录-------------------------
	
	/**
	* @Title: log
	* @Description: 多业务动态日志
	* @param @param businessLogTypeEnum
	* @param @param info    参数
	* @return void    返回类型
	* @throws
	* @author Orange
	* @date 2019年1月21日
	*/
	public static void log(BusinessLogTypeEnum businessLogTypeEnum, Object...info) {
		MDC.put("businessName", businessLogTypeEnum.getType());
		Logger logger = LoggerFactory.getLogger("business_log");
		logger.info("businessName={}, info={}", businessLogTypeEnum.getType(), info);
		MDC.remove(businessLogTypeEnum.getType());
	}
	
	
	
	/**
	* @Title: log
	* @Description: 多业务动态日志
	* @param @param businessLogTypeEnum
	* @param @param e
	* @param @param info    参数
	* @return void    返回类型
	* @throws
	* @author Orange
	* @date 2019年1月21日
	*/
	public static void log(BusinessLogTypeEnum businessLogTypeEnum, Throwable e, Object...info) {
		MDC.put("businessName", businessLogTypeEnum.getType());
		Logger logger = LoggerFactory.getLogger("business_log");
		logger.info("businessName={}, exception={} ,info={}", businessLogTypeEnum.getType(), throwable2String(e),info);
		MDC.remove(businessLogTypeEnum.getType());
	}
	
	
	
	/**
	* @Title: warnLog
	* @Description: 警告日志
	* @param @param e
	* @param @param info    参数
	* @return void    返回类型
	* @throws
	* @author Orange
	* @date 2019年1月26日
	*/
	public static void warnLog(Throwable e,Object...info) {
		//系统级别日志不打印
		if(checkBaseBusinessException(e)) {
			log(e,info);
		}else {
			Logger logger = LoggerFactory.getLogger("error_log");
			logger.warn("[System Exception] exception={} ,info={}", throwable2String(e),info);
		}
	}

	public static void warnLog(String warnInfo){
		Logger logger = LoggerFactory.getLogger("error_log");
		logger.warn(warnInfo);
	}

}
