Throwable.getStackTrace()
Return. Each element represents a separate stack frame. All stack frames (except for that stack frame at the top of the stack) represent a method call. The frame at the top of the stack represents the execution point that generated the stack trace. Typically, this is the point that creates the throwable that corresponds to the stack trace. Construction method Information:
Public Stacktraceelement (String declaringclass, string methodName, string fileName, int linenumber)
Parameter description:
-
-
declaringClass
-The fully qualified name of the class that contains the execution point represented by the stack trace element
-
methodName
- The method name, which contains the execution point represented by the stack trace element
-
fileName
-The file name that contains the execution point represented by the stack trace element, and if the information is not available, the parameter is null
-
lineNumber
-The line number of the source code line that contains the execution point represented by the stack trace element, or negative if this information is not available. A value of 2 means that the method that contains the execution point is a native method
When we develop on Android, we can use Stacktraceelement class to implement the print format of our logs:
Private void Printlog () {
stacktraceelement stack = (new Throwable ()). Getstacktrace () [1 ]; StringBuilder builder = new StringBuilder ();
Builder.append ("<-> file:" + stack.getfilename ());
Builder.append ("line number:" + stack.getlinenumber ());
Builder.append ("<-> method:" + stack.getmethodname ());
Builder.append ("<-> information:" + paramstring);
LOG.I ("MyLog", builder.tostring ());
}
If we debug our app through a log, we can find the class name, line number, method name, etc. where the problem occurs, by looking at the log.
Print Android logs with stacktraceelement design