Android——自定义Log显示

Android——Log显示日志

显示效果如下:

Android——自定义Log显示_第1张图片

具体代码实现:

public class LogUtil {
    private static String TAG = "zss";
    private static boolean isDebug = true;
    public static final int DEBUG = 3;
    public static final int ERROR = 6;

    public static void init(boolean debug, String tag) {
        isDebug = debug;
        TAG = tag;
    }

    public static void logDebug(String msg, Object... params) {
        logDebug(null, msg, params);
    }

    public static void logDebug(String tag, String msg, Object[] params) {
        if (!isDebug) return;
        LogText.log(getFinalTag(tag), String.format(msg, params), DEBUG);
    }

    public static void logError(Throwable e) {
        logError(null, e);
    }

    public static void logError(String tag, Throwable e) {
        if (!isDebug) return;
        LogText.log(getFinalTag(tag), String.format("%s", e.toString()), ERROR, e);
    }

    public static void logError(String msg, Object... params) {
        logError(null, msg, params, null);
    }

    public static void logError(String tag, String msg, Object[] params, Throwable th) {
        if (!isDebug) return;
        LogText.log(getFinalTag(tag), String.format(msg, params), ERROR, th);
    }

    private static String getFinalTag(String tag) {
        if (!TextUtils.isEmpty(tag)) {
            return tag;
        }
        return TAG;
    }

    private static class LogText {
        private static final char TOP_LEFT_CORNER = '┌';
        private static final char BOTTOM_LEFT_CORNER = '└';
        private static final char MIDDLE_CORNER = '├';
        private static final char HORIZONTAL_LINE = '│';
        private static final String DOUBLE_DIVIDER = "────────────────────────────────────────────────────────";
        private static final String SINGLE_DIVIDER = "┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄";
        private static final String TOP_BORDER = TOP_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER;
        private static final String BOTTOM_BORDER = BOTTOM_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER;
        private static final String MIDDLE_BORDER = MIDDLE_CORNER + SINGLE_DIVIDER + SINGLE_DIVIDER;

        private String mTag;

        public LogText(String mTag) {
            this.mTag = mTag;
        }

        public static void log(String tag, String content, int debugIndex) {
            log(tag, content, debugIndex, null);
        }

        public static void log(String tag, String content, int debugIndex, Throwable th) {
            LogText logText = new LogText(tag);
            logText.setup(content, debugIndex, th);
        }

        public void setup(String content, int debugType, Throwable th) {
            setUpHeader(debugType);
            setUpContent(content, debugType, th);
            setUpFooter(debugType);
        }

        private void setUpHeader(int debugType) {
            Log.println(debugType, mTag, TOP_BORDER);
        }

        private void setUpFooter(int debugType) {
            Log.println(debugType, mTag, BOTTOM_BORDER);
        }

        public void setUpContent(String content, int debugType, Throwable th) {
            StackTraceElement targetStackTraceElement = getTargetStackTraceElement();
            Log.println(debugType, mTag, HORIZONTAL_LINE + " " + targetStackTraceElement.getClassName() + "." + targetStackTraceElement.getMethodName() + " (" + targetStackTraceElement.getFileName() + ":"
                    + targetStackTraceElement.getLineNumber() + ")");
            Log.println(debugType, mTag, MIDDLE_BORDER);
            Log.println(debugType, mTag, HORIZONTAL_LINE + " " + content);
            if ((null != th) && (th.getStackTrace().length > 0)) {
                Log.println(debugType, mTag, HORIZONTAL_LINE + " " + th.getStackTrace()[0].toString());
            }
        }

        private StackTraceElement getTargetStackTraceElement() {
            // find the target invoked method
            StackTraceElement targetStackTrace = null;
            boolean shouldTrace = false;
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            for (StackTraceElement stackTraceElement : stackTrace) {
                boolean isLogMethod = stackTraceElement.getClassName().equals(LogUtil.class.getName());
                if (shouldTrace && !isLogMethod) {
                    targetStackTrace = stackTraceElement;
                    break;
                }
                shouldTrace = isLogMethod;
            }
            return targetStackTrace;
        }
    }
}

使用方式:

 		LogUtil.logDebug("======xxxxx=====");
        LogUtil.logError("======sssss======");
        LogModelTest modelTest = new LogModelTest(1, "zss");
        LogUtil.logDebug("新添加的内容:%s", modelTest.toString());
      	try {
          	   String aa = null;
         	   aa.toString();
       	 } catch (Exception e) {
              LogUtil.logError(e.toString());
         }

参考地址:

张鸿洋大神的代码:

http://blog.csdn.net/lmj623565791/article/details/52506545

Logger源码:

https://github.com/orhanobut/logger

你可能感兴趣的:(android)