Ultra practical android custom log log output tool class

  • 2021-11-01 04:53:40
  • OfStack

android customizes the log log output tool, which has the following advantages:

1 Filling this in the first parameter of LogUtlis method can output the name of the current class, especially when using anonymous inner classes.
Such as: LogUtils. i (this, "This is a useful logging tool class") or LogUtils. i (class name. class, "This is a useful logging tool class").
Effect: For example, I directly LogUtils. i (this, "logTest") in MainActivity, with my favorite logo, the result can be output as follows
"zhang---MainActivity: logTest", easy to debug and watch log.

2 can cooperate with androidStudio build file custom variables to control the output of different versions of log.

Use steps:

1 Define variable name in build. gradle under app directory, and finish the project under make or build.


 /**
  *  release  Under buildConfigField  For false Will be blocked log Output 
  */
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
      buildConfigField "boolean","LOG_DEBUG","true"
    }

    debug{
      minifyEnabled false
      buildConfigField "boolean","LOG_DEBUG","true"
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

    }
  }

2 As defined in the onCreate method in the applied application


/**
 *  BuildConfig.LOG_DEBUG  Get build.gradle Custom in the log Control variable 
 */
      if (BuildConfig.LOG_DEBUG) {
      LogUtils.isShowLog = true;
    } else {
      LogUtils.isShowLog = false;
    }

LogUtils


/**
 *  @ Founder    zsh
 *  @ Creation time   2017/1/17 10:23
 *  @ Describe     ${log Controlled tool class }
 *  
 *  @ Updater    $Author
 *  @ Update time   $Date
 *  @ Update description   ${TODO}
 */

public class LogUtils {

  /** Log Output control switch  */
  public static boolean isShowLog = true;
  /**  Developers define themselves, and I use my last name to log Adj.  */
  public static final String selfFlag = "zhang---------";
  public static void i(Object objTag, String msg) {
    if (!isShowLog) {
      return;
    }
    String tag;

    //  If objTag Yes String Is used directly 
    //  If objTag No String Is used, its class name is used 
    //  If you are in an anonymous inner class, write this The class is not recognized, so 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 = objTag.getClass().getName();
      String[] split = tag.split("\\.");
      tag=split[split.length-1].split("\\$")[0];
    }

    if (TextUtils.isEmpty(msg)) {
      Log.i(selfFlag.concat(tag), " The log The output information is empty ");
    } else {
      Log.i(selfFlag.concat(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 = objTag.getClass().getName();
      String[] split = tag.split("\\.");
      tag=split[split.length-1].split("\\$")[0];
    }

    if (TextUtils.isEmpty(msg)) {
      Log.e(selfFlag.concat(tag), " The log The output information is empty ");
    } else {
      Log.e(selfFlag.concat(tag), msg);
    }
  }

  /**
   *  Verbose 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 = objTag.getClass().getName();
      String[] split = tag.split("\\.");
      tag=split[split.length-1].split("\\$")[0];
    }

    if (TextUtils.isEmpty(msg)) {
      Log.v(selfFlag.concat(tag), " The log The output information is empty ");
    } else {
      Log.v(selfFlag.concat(tag), msg);
    }
  }

  /**
   *  Debugging information for warnings 
   * @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 = objTag.getClass().getName();
      String[] split = tag.split("\\.");
      tag=split[split.length-1].split("\\$")[0];
    }

    if (TextUtils.isEmpty(msg)) {
      Log.w(selfFlag.concat(tag), " The log The output information is empty ");
    } else {
      Log.w(selfFlag.concat(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 = objTag.getClass().getName();
      String[] split = tag.split("\\.");
      tag=split[split.length-1].split("\\$")[0];
    }
    if (TextUtils.isEmpty(msg)) {
      Log.d(selfFlag.concat(tag), " The log The output information is empty ");
    } else {
      Log.d(selfFlag.concat(tag), msg);
    }
  }
}

Related articles: