Android Development Log Unified Management

Source: Internet
Author: User

In the development, we usually want to do a unified management of the output of the log, the following is recommended for everyone a log output class, in the development phase only need to set the debug constant to True, the production environment will debug to false to control the output of the log. Do not say anything, the need for friends to take directly.

 PackageCom.android.util;/*** Log Output Unified Management * *@authorQilian Mountain * @date 2015-04-27 *@version1.0 **/ Public classLog {Private Static FinalString TAG = "Com.android.app"; Private Static Final BooleanDEBUG =true; Private StaticString Getfunctionname () {stacktraceelement[] sts=Thread.CurrentThread (). Getstacktrace (); if(sts = =NULL) {            return NULL; }         for(stacktraceelement st:sts) {if(St.isnativemethod ()) {Continue; }            if(St.getclassname (). Equals (Thread.class. GetName ())) {                Continue; }            if(St.getclassname (). Equals (Log.class. GetName ())) {                Continue; }            return"[" + Thread.CurrentThread (). GetName () + "(" + thread.currentthread (). GetId () + "):" + st.getfilename () + ":" + St.getlin ENumber () + "]"; }        return NULL; }    Private Staticstring CreateMessage (String msg) {string functionname=Getfunctionname (); String message= (FunctionName = =NULL? Msg: (functionname + "-" +msg)); returnmessage; }     Public Static voidI (String msg) {if(DEBUG) {String message=createmessage (msg);        ANDROID.UTIL.LOG.I (TAG, message); }    }     Public Static voidV (String msg) {if(DEBUG) {String message=createmessage (msg);        ANDROID.UTIL.LOG.V (TAG, message); }    }     Public Static voidd (String msg) {if(DEBUG) {String message=createmessage (msg);        ANDROID.UTIL.LOG.D (TAG, message); }    }     Public Static voide (String msg) {if(DEBUG) {String message=createmessage (msg);        ANDROID.UTIL.LOG.E (TAG, message); }    }     Public Static voidW (String msg) {if(DEBUG) {String message=createmessage (msg);        ANDROID.UTIL.LOG.W (TAG, message); }    }     Public Static voide (Exception e) {if(DEBUG) {StringBuffer sb=NewStringBuffer (); String name=Getfunctionname (); Stacktraceelement[] STS=E.getstacktrace (); if(Name! =NULL) {sb.append (name+ "-" + E + "\ r \ n"); } Else{sb.append (e+ "\ r \ n"); }            if(STS! =NULL&& sts.length > 0) {                 for(stacktraceelement st:sts) {if(St! =NULL) {sb.append ("[" + st.getfilename () + ":" + st.getlinenumber () + "]\r\n");        }}} android.util.log.e (TAG, sb.tostring ()); }    }}

Android Development Log Unified Management

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.