import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.junit.Test; /** * * Description: 异常信息打印(controller继承此类即可调用) * * @author: Byron Wang * @version: V1.0 */ public class CommonController { /** * Description:获取子类调用处方法名字 * @return 调用处方法名 */ public String getCurMethodName() { // 获取调用处方法的名称 return Thread.currentThread().getStackTrace()[2].getMethodName(); } public static final int ENGLISH = 0; public static final int CHINESE = 1; private static final int MSG_NUM = 5; private static final int NOTICE_INDEX = 0; private static final int HAPPEN_INDEX = 1; private static final int CATCH_INDEX = 2; private static final int MSGE_INDEX = 3; private static final int DATETIME_INDEX = 4; /** * Description: 获取异常日志信息(本方法不适用于单元测试中) * * @param exception * @param lang * @return */ public static String getExceptionLogMsg(Exception exception, int lang) { String[] header = { "DEBUG MESSAGE:::", "exception happen: [", "exception catch : [", "\tmessage: [", "datetime: ", "异 常 提 示 信 息:::", "异常发生: [", "异常捕获: [", "\t异常信息: [", "日期时间: " }; String lineTail = "]\n\t\t"; int baseindex = lang * MSG_NUM; StackTraceElement[] stes = exception.getStackTrace(); StackTraceElement happenTrace = stes[0]; StackTraceElement catchTrace = stes[stes.length - 1]; // 获取异常信息,若为空则返回异常名称 String message = exception.getMessage(); if (message == null || message.trim().length() == 0 || "null".equalsIgnoreCase(message)) { message = exception.getClass().getName(); } StringBuilder builder = new StringBuilder(header[baseindex + NOTICE_INDEX]); String nowStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); builder.append(header[baseindex + DATETIME_INDEX]).append(nowStr); builder.append(header[baseindex + MSGE_INDEX]) .append(message).append(lineTail); builder.append(header[baseindex + HAPPEN_INDEX]) .append(joinExceptionPosition(happenTrace)); builder.append(header[baseindex + CATCH_INDEX]) .append(joinExceptionPosition(catchTrace)); return builder.toString(); } /** * Description: 拼接异常位置 * * @param element * @return * */ private static String joinExceptionPosition(StackTraceElement element) { String lineTail = "]\n\t\t"; String seprator = ".."; StringBuilder builder = new StringBuilder(); builder.append(element.getClassName()).append(seprator) .append(element.getMethodName()).append(seprator) .append(element.getLineNumber()).append(lineTail); return builder.toString(); } /** * TEST * * @throws IOException */ public static void getException() throws IOException { throw new IOException("解析参数出错"); } public static void getException2() throws IOException { getException(); } @Test public void testException() { try { getException2(); } catch (IOException e) { e.printStackTrace(); System.out.println(getExceptionLogMsg(e, CHINESE)); } } @Test public void testExceptionNoMsg() { try { throw new StringIndexOutOfBoundsException(); } catch (StringIndexOutOfBoundsException e) { e.printStackTrace(); System.out.println(getExceptionLogMsg(e, CHINESE)); } } public static void main(String[] args) { try { throw new StringIndexOutOfBoundsException(); } catch (Exception e) { e.printStackTrace(); System.out.println(getExceptionLogMsg(e, CHINESE)); } // try { // getException2(); // } catch (IOException e) { // e.printStackTrace(); // System.out.println(getExceptionLogMsg(e, CHINESE)); // } } }