java 一个日志记录异常信息的小工具

java 一个日志记录异常信息的小工具

package com.crm.mess.util;

/**
 * 
 * @author liangrui
 * @2015/1/12
 *
 */
public class ExceptionsLog {

	public synchronized static String logClzInfo(Exception e) {
		StringBuffer sb = new StringBuffer();
		sb.append(e.getClass() + " " + e.getMessage() + "\n");
		StackTraceElement[] stackTraceElement = e.getStackTrace();
		for (StackTraceElement traceElement : stackTraceElement) {
			sb.append("\tat " + traceElement + "\n");
		}
		return sb.toString();
	}

}


package org.pass;

public class GetClz {
	public static void main(String[] args) {
		c();

	}

	public static void c() {
		try {
			int c = 10 / 0;
		} catch (Exception e) {
			String estr = ExceptionsLog.logClzInfo(e);
			System.out.println("estr" + ":" + estr);
			e.printStackTrace();
		}
	}

}

/*output:
 * 
 * java.lang.ArithmeticException: / by zero
	at org.pass.GetClz.c(GetClz.java:13)
	at org.pass.GetClz.main(GetClz.java:5)
estr:class java.lang.ArithmeticException / by zero
	at org.pass.GetClz.c(GetClz.java:13)
	at org.pass.GetClz.main(GetClz.java:5)
 * */


你可能感兴趣的:(开发原创utils)