Android custom log log output tool, this tool class has the following advantages:
1 Fill in this in the first parameter of the LogUtlis method to output the name of the current class, especially in anonymous internal classes, you can also output the name of the current class.
Such as: (this,"This is a practical log tool class") or (class name.class,"This is a practical log tool class").
Effect: For example, I directly (this, "logTest") in MainActivity, and cooperate with my favorite logo, and the result can be output as
"zhang————MainActivity: logTest", convenient for debugging and reading logs.
2 You can use custom variables in the build file in AndroidStudio to control whether the output of different versions of logs is performed.
Steps to use:
1 Define the variable name in the app directory and write the project under make or build.
/** * If buildConfigField under release is false, the log output will be blocked. */ buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(''), '' buildConfigField "boolean","LOG_DEBUG","true" } debug{ minifyEnabled false buildConfigField "boolean","LOG_DEBUG","true" proguardFiles getDefaultProguardFile(''), '' } }
2 Define this in the onCreate method in the applied application
/** * BuildConfig.LOG_DEBUG Get the custom log control variable */ if (BuildConfig.LOG_DEBUG) { = true; } else { = false; }
LogUtils
/** * @creator zsh * @Creation time 2017/1/17 10:23 * @Description ${log control tool class} * * @Updator $Author * @Update time $Date * @Update Description ${TODO} */ public class LogUtils { /** Control switch for Log output */ public static boolean isShowLog = true; /** Developers define it themselves, I use my last name to log */ public static final String selfFlag = "zhang---------"; public static void i(Object objTag, String msg) { if (!isShowLog) { return; } String tag; // If objTag is a String, use it directly // If objTag is not a String, use its class name // If you are in an anonymous internal class, if you write this, you cannot recognize the class, so you can get the full class name of the current object to separate it. if (objTag instanceof String) { tag = (String) objTag; } else if (objTag instanceof Class) { tag = ((Class) objTag).getSimpleName(); } else { tag = ().getName(); String[] split = ("\\."); tag=split[-1].split("\\$")[0]; } if ((msg)) { ((tag), "The log output information is empty"); } else { ((tag), msg); } } /** * Error debugging information * @param objTag * @param msg */ public static void e(Object objTag, String msg) { if (!isShowLog) { return; } String tag; if (objTag instanceof String) { tag = (String) objTag; } else if (objTag instanceof Class) { tag = ((Class) objTag).getSimpleName(); } else { tag = ().getName(); String[] split = ("\\."); tag=split[-1].split("\\$")[0]; } if ((msg)) { ((tag), "The log output information is empty"); } else { ((tag), msg); } } /** * Detailed output debugging * @param objTag * @param msg */ public static void v(Object objTag, String msg) { if (!isShowLog) { return; } String tag; if (objTag instanceof String) { tag = (String) objTag; } else if (objTag instanceof Class) { tag = ((Class) objTag).getSimpleName(); } else { tag = ().getName(); String[] split = ("\\."); tag=split[-1].split("\\$")[0]; } if ((msg)) { ((tag), "The log output information is empty"); } else { ((tag), msg); } } /** * Warning debugging information * @param objTag * @param msg */ public static void w(Object objTag, String msg) { if (!isShowLog) { return; } String tag; if (objTag instanceof String) { tag = (String) objTag; } else if (objTag instanceof Class) { tag = ((Class) objTag).getSimpleName(); } else { tag = ().getName(); String[] split = ("\\."); tag=split[-1].split("\\$")[0]; } if ((msg)) { ((tag), "The log output information is empty"); } else { ((tag), msg); } } /** * debug output debugging * @param objTag * @param msg */ public static void d(Object objTag, String msg) { if (!isShowLog) { return; } String tag; if (objTag instanceof String) { tag = (String) objTag; } else if (objTag instanceof Class) { tag = ((Class) objTag).getSimpleName(); } else { tag = ().getName(); String[] split = ("\\."); tag=split[-1].split("\\$")[0]; } if ((msg)) { ((tag), "The log output information is empty"); } else { ((tag), msg); } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.