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使用教程;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,149评论 25 709
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,663评论 2 45
  • 碧水清泓小寒深 寂廖静庐茅烟轻 瘦鸟惊飞晚钟远 近乡情怯独一人
    白云老愚阅读 1,202评论 0 0
  • 文/孤鸟差鱼 一滴水的洗涤 我对不起它给的干净 一场雨的冲动 我对不起它给的稳重 一次风的暴戾 我对不起它给的温柔...
    孤鸟差鱼阅读 1,014评论 4 5
  • 一、什么是国债逆回购? 国债逆回购是开通证券账户才可以做的一个投资品种,需要在相应的股票交易软件里进行操作。它是一...
    紫竹姑娘阅读 4,280评论 0 1