Exception e = new Exception(“this is a log”); e.printStackTrace();
———————————
StringBuilder sb=new StringBuilder(“”);
Exception e = new Exception(“hhhhhhhhhhhhhhhhhhh”);
StackTraceElement[] trace = e.getStackTrace();;
for (int i=0; i < trace.length; i++)
sb.append(“\tat ” + trace[i]);
logger.error(“who invoke me\n”+sb.toString());
Exception e = new Exception(“hhhhhhhhhhhhhhhhhhh”);
StackTraceElement[] trace = e.getStackTrace();;
for (int i=0; i < trace.length; i++)
sb.append(“\tat ” + trace[i]);
logger.error(“who invoke me\n”+sb.toString());
—————————————-
source-code:
public class A {
public A() {}
private static void printStackTrace() {
output:
A.printStackTrace(A.java:5)
A.methodB(A.java:16)
A.methodA(A.java:15)
A.main(A.java:20)
Note:
StackTraceElement.java
另外下面这些方法也可以:
new Exception(“hello”).printStackTrace();
Thread.getStackTrace()/Thread.getAllStackTraces() since jdk 1.5
Thread.currentThread().dumpStack()
参考资料:
1. 线程运行栈信息的获取
2. jdk 1.7 Thread.java
//Note:dumpThreads is a native method