Print Android logs with stacktraceelement design

Source: Internet
Author: User
Tags print format stack trace

    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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.