package com.base; import java.beans.IntrospectionException; import java.lang.reflect.InvocationTargetException; import org.apache.log4j.Logger; import com.platform.controller.DataModelController; import com.platform.utils.Bean2MapUtils; import com.platform.utils.Configs; import com.platform.utils.Constant; /** * <一句话功能简述> * <功能详细描述> * @author chen * @version [版本号,2016年9月8日] * @see [相关类/方法] * @since [产品/模块版本] */ @SuppressWarnings("serial") public class CustomException extends Exception { /** log4j */ @SuppressWarnings("static-access") public static Logger log = Configs.DAILY_ROLLING_LOGGER.getLogger(CustomException.class); /** 自定义异常信息-错误信息 */ private String msg; /** 自定义异常信息-错误代码 */ private String code; /** 操作对象 */ private Object[] objArray; /** 异常 */ private Throwable cause; public CustomException() { super(); } /** * @功能 将异常记录进文件 * @param code 异常编码 * @param msg 自定义异常信息 * @param e * @param obj */ public CustomException(String code,Exception e,Object... obj) { super(code); StringBuffer sbuf= new StringBuffer(); this.code = code; sbuf.append(code); sbuf.append("\r\n"); msg = Resource.getProperties().get(code); // 记录自定义的 异常 if (null != msg) { sbuf.append(msg); sbuf.append("\r\n"); } // 记录原始的异常 if (null != e) { sbuf.append(e.getMessage()); sbuf.append("\r\n"); StackTraceElement[] array = e.getStackTrace(); cause = e.getCause(); for (StackTraceElement stackTraceElement : array) { sbuf.append(stackTraceElement.toString()); sbuf.append("\r\n"); } } //记录 出现异常时 当前的对象 if (null != obj) { Object[] array = obj; sbuf.append("Object[] size : "); sbuf.append(array.length); sbuf.append("\r\n"); int forSize = 0; if (Constant.CustomException_log_object_size < array.length) { forSize = Constant.CustomException_log_object_size; } else { forSize = array.length; } for (int i = 0; i < forSize; i++) { sbuf.append(array[i]); sbuf.append("\r\n"); try { sbuf.append(Bean2MapUtils.convertBean(array[i])); sbuf.append("\r\n"); } catch (IllegalAccessException | InvocationTargetException | IntrospectionException e1) { } } if (forSize == Constant.CustomException_log_object_size) { sbuf.append("......"); sbuf.append("\r\n"); } } else { sbuf.append("null"); sbuf.append("\r\n"); } sbuf.append("\r\n"); // 是否 写入 文件 log.error(sbuf.toString()); } /** * @功能 获得msg * @return msg */ public String getMsg() { return msg; } /** * @return the code */ public String getCode() { return code; } /** * @功能 获得objArray * @return objArray */ public Object[] getObjArray() { return objArray; } /** * @功能 获得cause * @return cause */ public Throwable getCause() { return cause; } }