Andorid 自定义LogUtil

概述

    开发过程中打日志已成为我们平时Debug调试不可缺少的一部分,Android SDK给我们也提供了很不错的工具类,并且分了不同的日志级别:Log.v() Log.d() Log.i() Log.w() and Log.e() 分别对应 VERBOSE,DEBUG,INFO, WARN, ERROR,其中Verbose不会在release版本中被编译进应用程序包中,而Debug日志根据Android API说会在运行时被去掉,另外的三个则会一直被保留,那么有没有方法把所有日志在发布的时候全都去掉呢?答案是肯定的。

    发布时去除日志 Android的BuildConfig有一个很合适的DEBUG可以用,它在你发布release版本,这个bool值自动变为false;所以我们可以利用这一点,重新定义写Log的方法,我用的是Android Studio开发,在Gradle中可以定义许多BuildConfigField,供我们使用,这篇文章就不该输Gradle了,有兴趣的同学可以分享学习学习。

public class LogUtil {

private static String fileName;
private static String methodName;
private static int lineNumber;
private static String className;
private static String TAG;


public static void setTag(String tag) {
    TAG = tag;
}

public static void e(String msg) {
    if (isDebugable()) {
        Throwable throwable = new Throwable();
        String logMsg = createLog(throwable, msg);
        if (!TextUtils.isEmpty(TAG)) {
            className = TAG;
        }
        Log.e(className, logMsg);
        TAG = null;
    }
}

public static void json(String json) {
    try {
        if (isDebugable()) {
            JSONObject jo = new JSONObject(json);
            String msg = jo.toString(2);

            Throwable throwable = new Throwable();
            String logMsg = createLog(throwable, msg);
            if (!TextUtils.isEmpty(TAG)) {
                className = TAG;
            }
            Log.e(className, logMsg);
            TAG = null;
        }
    } catch (JSONException e) {
        createLog(e, e.getMessage());
    }
}

public static void i(String msg) {
    if (isDebugable()) {

        Throwable throwable = new Throwable();
        String logMsg = createLog(throwable, msg);
        if (!TextUtils.isEmpty(TAG)) {
            className = TAG;
        }
        Log.i(className, logMsg);
        TAG = null;
    }
}

public static void v(String msg) {
    if (isDebugable()) {
        Throwable throwable = new Throwable();
        String logMsg = createLog(throwable, msg);
        if (!TextUtils.isEmpty(TAG)) {
            className = TAG;
        }
        Log.v(className, logMsg);
        TAG = null;
    }
}

public static void w(String msg) {
    if (isDebugable()) {
        Throwable throwable = new Throwable();
        String logMsg = createLog(throwable, msg);
        if (!TextUtils.isEmpty(TAG)) {
            className = TAG;
        }
        Log.w(className, logMsg);
    }
}

private static boolean isDebugable() {
    return BuildConfig.DEBUG;
}

public static String createLog(Throwable throwable, String msg) {
    if (throwable != null) {
        StackTraceElement sElements = throwable.getStackTrace()[1];//方法栈
        fileName = sElements.getFileName();//点击定位到指定的位置
        methodName = sElements.getMethodName();
        lineNumber = sElements.getLineNumber();
        int index = fileName.indexOf(".");
        className = fileName.substring(0, index);
        return createLog(fileName, methodName,
                lineNumber, msg);

    }
    return "The string is null";
}

public static String createLog(String className, String methodName, int lineNumber, String log) {
    StringBuffer buffer = new StringBuffer();
    buffer.append(methodName);
    buffer.append("(").append(className).append(":").append(lineNumber).append(")");
    buffer.append("---> " + log);
    return buffer.toString();
}
}

    StackTraceElement sElements = throwable.getStackTrace()[1];这条语句是最重要的,叫方法栈,即方法调用的容器,根据后进先出,方法压栈等;下片文章将介绍StackTrace以及StackTraceElement使用教程;

你可能感兴趣的:(Andorid 自定义LogUtil)