Unified management of Android development logs and unified management of android
During development, we usually need to manage the log output in a unified manner. Below we recommend a log output class. In the development stage, we only need to set the DEBUG constant to true, set DEBUG to false in the production environment to control log output. If you don't want to talk about anything, you need to take it directly.
Package com. android. util;/*** unified Log output management ** @ author Qilian Mountains * @ date 2015-04-27 * @ version 1.0 **/public class Log {private static final String TAG = "com. android. app "; private static final boolean DEBUG = true; private static String getFunctionName () {StackTraceElement [] sts = Thread. currentThread (). getStackTrace (); if (sts = null) {return null;} for (StackTraceElement st: sts) {if (st. isNative Method () {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. getLineNumber () + "]";} return null;} private static String createMessage (String msg) {String functionName = getFunct IonName (); String message = (functionName = null? Msg: (functionName + "-" + msg); return message;} public static void I (String msg) {if (DEBUG) {String message = createMessage (msg ); android. util. log. I (TAG, message) ;}} public static void v (String msg) {if (DEBUG) {String message = createMessage (msg); android. util. log. v (TAG, message) ;}} public static void d (String msg) {if (DEBUG) {String message = createMessage (msg); android. util. L Og. d (TAG, message) ;}} public static void e (String msg) {if (DEBUG) {String message = createMessage (msg); android. util. log. e (TAG, message) ;}} public static void w (String msg) {if (DEBUG) {String message = createMessage (msg); android. util. log. w (TAG, message) ;}} public static void e (Exception e) {if (DEBUG) {StringBuffer sb = new StringBuffer (); String name = getFunctionName (); StackTrace Element [] 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 ());}}}