Frequently used Logutil in Android development

Source: Internet
Author: User

Log is used frequently during the development process. One way we often write is to define a Logutil tool class.

    Private Static BooleanLOGV =true;Private Static BooleanLOGD =true;Private Static BooleanLogi =true;Private Static BooleanLOGW =true;Private Static BooleanLOGE =true; Public Static void v(string tag, string mess) {if(LOGV)    {LOG.V (tag, mess);} } Public Static void D(string tag, string mess) {if(LOGD)    {LOG.D (tag, mess);} } Public Static void I(string tag, string mess) {if(Logi)    {LOG.I (tag, mess);} } Public Static void W(string tag, string mess) {if(LOGW)    {LOG.W (tag, mess);} } Public Static void e(string tag, string mess) {if(LOGE)    {LOG.E (tag, mess);} }

This kind of tool class can classify log and selectively close log when the application is advertised. More convenient.
The problem is that, like using log directly, you need to define a tag, which is often used to define a static string constant tag in each class that needs to be hit log, assigning a class name.

Assume. When refactoring, forget to change the tag so that you may be confused when looking at log.

Of course, there are also very many people who will be easy to figure. Directly with System.out.println (str); output.
When I looked at Volleylog two days ago, I learned a new way.

    Private StaticStringGettag() {stacktraceelement[] trace =NewThrowable (). Fillinstacktrace (). Getstacktrace (); String Callingclass =""; for(inti =2; i < trace.length; i++) {class<?> clazz = Trace[i].getclass ();if(!clazz.equals (Logutil.class))                {Callingclass = Trace[i].getclassname (); Callingclass = callingclass.substring (callingclass. LastIndexOf ('. ') +1); Break; }        }returnCallingclass; }

This method can get directly to the caller's class name.

Use for example the following:

     Public Static void v(String Mess) {if(LOGV)    {LOG.V (Gettag (), mess);} } Public Static void D(String Mess) {if(LOGD)    {LOG.D (Gettag (), mess);} } Public Static void I(String Mess) {if(Logi)    {log.i (Gettag (), mess);} } Public Static void W(String Mess) {if(LOGW)    {LOG.W (Gettag (), mess);} } Public Static void e(String Mess) {if(LOGE)    {LOG.E (Gettag (), mess);} }

So there is no need to define tags in the class.

When you debug a program, we often print some information, including the method name/line number, and so on. Here's a way to save the hassle:

 private static String buildMessage(String msg) { StackTraceElement[] trace = new Throwable().fillInStackTrace() .getStackTrace(); String caller = ""; for (int i = 2; i < trace.length; i++) { Class<?

> clazz = trace[i].getClass(); if (!clazz.equals(LogUtil.class)) { caller = trace[i].getMethodName(); break; } } return String.format(Locale.US, "[%d] %s: %s", Thread.currentThread() .getId(), caller, msg); }

Usage such as the following:

     Public Static void v(String Mess) {if(LOGV)    {LOG.V (Gettag (), buildmessage (Mess));} } Public Static void D(String Mess) {if(LOGD)    {LOG.D (Gettag (), buildmessage (Mess));} } Public Static void I(String Mess) {if(Logi)    {log.i (Gettag (), buildmessage (Mess));} } Public Static void W(String Mess) {if(LOGW)    {LOG.W (Gettag (), buildmessage (Mess));} } Public Static void e(String Mess) {if(LOGE)    {LOG.E (Gettag (), buildmessage (Mess));} }

This is very convenient to print the log every time, directly type: LOGUTIL.V (msg);
Do not need to control the tag, method name. Also the thread ID and so on information

One more step is to format the contents of the MSG

buildMessage(StringObject... args)

Finally, explain. Assuming a large number of logs like this will affect the performance of the program. So this method is only convenient for debugging, and when it is published, it is possible to turn off the debug log.

Frequently used Logutil in Android development

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.