OpenDeclarationStackTraceElement[]java.lang.Throwable.getStackTrace() ProvidesprogrammaticaccesstothestacktraceinformationprintedbyprintStackTrace().Returnsanarrayofstacktraceelements,eachrepresentingonestackframe.Thezerothelementofthearray(assumingthearray'slengthisnon-zero)representsthetopofthestack,whichisthelastmethodinvocationinthesequence.Typically,thisisthepointatwhichthisthrowablewascreatedandthrown.Thelastelementofthearray(assumingthearray'slengthisnon-zero)representsthebottomofthestack,whichisthefirstmethodinvocationinthesequence. Somevirtualmachinesmay,undersomecircumstances,omitoneormorestackframesfromthestacktrace.Intheextremecase,avirtualmachinethathasnostacktraceinformationconcerningthisthrowableispermittedtoreturnazero-lengtharrayfromthismethod.Generallyspeaking,thearrayreturnedbythismethodwillcontainoneelementforeveryframethatwouldbeprintedbyprintStackTrace.Writestothereturnedarraydonotaffectfuturecallstothismethod. Returns: anarrayofstacktraceelementsrepresentingthestacktracepertainingtothisthrowable. Since: 1.4
package com.doctor.reflect;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* Call stack introspection
*
* @author sdcuike
*
* Created At 2016年8月29日 下午9:40:35
*/
public class CallStackIntrospectionDemo {
private static final MyLogger logger = new LoggerImpl();
public static void main(String[] args) {
logger.logRecord("hello");
IllegalArgumentException exception = new IllegalArgumentException("IllegalArgumentException");
logger.logProblem("throwable", exception);
}
public interface MyLogger {
// Types for log records
int ERROR = 0;
int WARNING = 100;
int STATUS = 200;
int DEBUG = 300;
int TRACE = 400;
void logRecord(String message);
void logProblem(String message, Throwable throwable);
}
public static class LoggerImpl implements MyLogger {
@Override
public void logRecord(String message) {
Throwable throwable = new Throwable();
log(message, throwable.getStackTrace()[1]);
}
@Override
public void logProblem(String message, Throwable throwable) {
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
throwable.printStackTrace(writer);
writer.flush();
log(message + out.toString(), throwable.getStackTrace()[0]);
}
private void log(String message, StackTraceElement stackTraceElement) {
String className = stackTraceElement.getClassName();
String methodName = stackTraceElement.getMethodName();
int lineNumber = stackTraceElement.getLineNumber();
System.out.println(String.join(" ", "模拟打印日志:", methodName, className, "" + lineNumber, message));
}
}
}
模拟打印日志: main com.doctor.reflect.CallStackIntrospectionDemo 36 hello 模拟打印日志: main com.doctor.reflect.CallStackIntrospectionDemo 38 throwablejava.lang.IllegalArgumentException: IllegalArgumentException at com.doctor.reflect.CallStackIntrospectionDemo.main(CallStackIntrospectionDemo.java:38)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有