Android Project log log output optimization

Source: Internet
Author: User

Overview

The Android development process often need to output log information to the console, some people are still in the form of LOG.I (tag,msg) or System.out.println (msg) mode? This article optimizes the log information output to achieve a fast location of the output log and the effect of the output specification log. The log output line shows the first line of the file in which the output log information is called, and can and clicks jump to the corresponding location.

General form of output logs

1, in the project directly call LOG.I (tag,msg) or println function, this is the practice of junior developers, not convenient for later maintenance and unified control.
2, in the project to encapsulate the output operation, in the encapsulation class unified call, this is the practice of mature developers, easy to control the log output unified, and in the release of the log output operation.
3, the second method of further optimization, let the log full display of the call information and facilitate the development process through the IDE directly to the call location. This article records the third type of optimization.

Encapsulating log Operations

Common log output operations uniformly put into a class, the project needs to output the log through this class to call, I believe everyone has a package, this step does not elaborate.

Log Information output optimization

We often encounter a problem in encapsulating log operations, that is, how tag is set, let the caller pass as a parameter or fixed use a constant light? My operation when the call location information is automatically recalled, the information is processed as a tag, look at the log when you see which file is the first line in the output log.

The following is a way to get tag information that can be directly copied into the code and called as text content through the PRINTLN function output can also achieve this effect.

/*** Run Stack class name     */    StaticString Stackclassname ="";/*** Run Stack     */    StaticStacktraceelement[] stacktraceelements;/*** Get the default tag     */    Private StaticStringGetdefaulttag(){Try{//Get current run task stack informationStacktraceelements = Thread.CurrentThread().Getstacktrace();//Traverse task stack information, get caller information and return             for(Stacktraceelement stacktraceelement:stacktraceelements) {stackclassname = stacktraceelement.GetClassName() +"";//Only get non-such invocation information under this item                if(!stackclassname.contains(Logutil.class.Getsimplename()) && stackclassname.contains("com.")) {return "("+ stacktraceelement.GetFileName() +":"+ stacktraceelement.Getlinenumber() +")"; }            }        }Catch(Exception e) {e.Printstacktrace(); }returnLogutil.class.Getsimplename(); }

Android Project log log output optimization

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.