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