Java调试打印堆栈信息

public class DebugUtil {


    public static void logCurrentThreadStack() {
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();
    StringBuffer sb = new StringBuffer();
    sb.append( "Thread stack trace:\n" );
if (stack != null && stack.length > 0) {
for ( int i = 0; i < stack.length; i ++ ) {
if ( i == 0 || i == 1 ) {
// 这个函数本身,不打印出来
continue;
}
StackTraceElement trace = stack[i];
sb.append( trace.getClassName() + "." +trace.getMethodName() + "(" + trace.getFileName() + ":" + trace.getLineNumber() + ")\n" );
}
}
ILog.getCommon().debug( sb );
    }
    
    /**
     * 放在正式环境中用于Debug
* 查看 * @param sign 用于在日志中定位(用户ID) * */ public static void logCurrentThreadStack(String sign) { StackTraceElement[] stack = Thread.currentThread().getStackTrace(); StringBuffer sb = new StringBuffer(); if (stack != null && stack.length > 0) { for ( int i = 2; i < stack.length; i ++ ) { StackTraceElement trace = stack[i]; sb.append( trace ); if( i != stack.length - 1 ) { sb.append("\n"); } } } ILog.diy().info("GM Debug=====================" + sign + " start========================\n" + sb); ILog.diy().info("GM Debug=====================" + sign + " end========================\n"); } }


你可能感兴趣的:(java)