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