美文网首页
Android定制Log中tag

Android定制Log中tag

作者: 沉默的菋道 | 来源:发表于2020-06-12 19:10 被阅读0次
public class MsgLoger {

    @SuppressLint("DefaultLocale")
    private static String getTag() {
        StackTraceElement caller = new Throwable().getStackTrace()[2];
        String tag = "%s.%s(L:%d)";
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
        return tag;
    }

    public static void d(String msg) {
        if (Constant.isDebug)
            Log.d(getTag(), msg);
    }

    public static void d(String tag, String msg) {
        if (Constant.isDebug)
            Log.d(tag, msg);
    }

    public static void i(String msg) {
        if (Constant.isDebug)
            Log.i(getTag(), msg);
    }

    public static void i(String tag, String msg) {
        if (Constant.isDebug)
            Log.i(tag, msg);
    }

    public static void w(String msg) {
        if (Constant.isDebug)
            Log.w(getTag(), msg);
    }

    public static void w(String tag, String msg) {
        if (Constant.isDebug)
            Log.w(tag, msg);
    }

    public static void w(Throwable tr) {
        if (Constant.isDebug)
            Log.w(getTag(), tr);
    }

    public static void w(String tag, Throwable tr) {
        if (Constant.isDebug)
            Log.w(tag, tr);
    }

    public static void e(String msg) {
        if (Constant.isDebug)
            Log.e(getTag(), msg);
    }

    public static void e(String tag, String msg) {
        if (Constant.isDebug)
            Log.e(tag, msg);
    }

    public static void e(String tag, String msg, Throwable e) {
        if (Constant.isDebug)
            Log.e(tag, msg);
    }

    public static void e(String tag, Throwable tr) {
        if (Constant.isDebug)
            Log.e(tag, tag, tr);
    }

    public static void e(Throwable tr) {
        if (Constant.isDebug)
            e(getTag(), tr);
    }
}
2020-08-20 15:13:47.381 3806-3806/com.test.log D/AboutActivity.onCreate(L:31): testLog

相关文章

网友评论

      本文标题:Android定制Log中tag

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