美文网首页
Andorid 自定义LogUtil

Andorid 自定义LogUtil

作者: 野火友烧不尽 | 来源:发表于2017-09-20 02:15 被阅读0次

概述

    开发过程中打日志已成为我们平时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

    概述 开发过程中打日志已成为我们平时Debug调试不可缺少的一部分,Android SDK给我们也提供了很不错的工...

  • LogUtil工具类

    步骤一:新建LogUtil类 public class LogUtil { private static int ...

  • 详细打印日志类

    【LogUtil 类】

  • 自定义View

    自定义View有多种形式,可以继承自View,也可以继承自ViewGroup,还可以直接继承Andorid系统现有...

  • 定制android的log工具

    定制android的log工具 编写工具类LogUtil

  • Kotlin的LogUtil

    最近在撸东西,调试的时候用Android原生的Log感觉很不爽,打的Log给我一种东倒西歪,凌乱的感觉。于是自己看...

  • LogUtil 的开关

    需求:LogUtil debuge模式开启 release模式关闭 在app build.gradle 文件中 b...

  • Andorid自定义控件(奶瓶)

    开发自定义控件的步骤:1、了解View的工作原理2、 编写继承自View的子类3、 为自定义View类增加属性4、...

  • Andorid :自定义Title控件

    0.Demo结构 1.新建自定义Title的布局文件 代码: 2.新建TitleLayout类 继承于Linear...

  • 史上最全Websocket通信测试

    H5和H5之间通信 H5和Andorid之间通信 Andorid和Andorid之间通信 H5通过http协议调用...

网友评论

      本文标题:Andorid 自定义LogUtil

      本文链接:https://www.haomeiwen.com/subject/qylnsxtx.html