LogUtil.java 3.23 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

package com.yidianling.common.tools;

import android.util.Log;


/**
 * LogUtil
 * Created by Jim on 2017/11/2 0002.
 */
@SuppressWarnings("unused")
public class LogUtil {

    private static String className;//类名
    private static String methodName;//方法名
    private static int lineNumber;//行数

    private static String lastMethodName;

20 21
    public static boolean debug = false;

konghaorui committed
22 23 24 25 26
    public static String TAG = "hzs";

    private LogUtil() {
    }

洪国微 committed
27 28 29 30
    public static void setPrint(boolean isPrint){
        debug = isPrint;
    }

konghaorui committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

    private static String printLog(String log) {
        //记录本地日志系统
        return "| " + Thread.currentThread().getName() + " | " +
                lastMethodName+"() -->" + methodName + "() | " +
                " (" + className + ":" + lineNumber + ") | " +
                log;
    }

    private static void getMethodNames(StackTraceElement[] sElements) {
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
        lastMethodName=sElements[2].getMethodName();
    }


    public static void e(String message) {
        if (debug) {
            getMethodNames(new Throwable().getStackTrace());
            Log.e(TAG, printLog(message));
        }

    }

    public static void e(String tag, String message) {
        if (debug) {
            getMethodNames(new Throwable().getStackTrace());
            Log.e(TAG + "_" + tag, printLog(message));
        }

    }


    public static void i(String message) {
        if (debug) {
            getMethodNames(new Throwable().getStackTrace());
            Log.i(TAG, printLog(message));
        }
    }

    public static void i(String tag, String message) {
        if (debug) {
            getMethodNames(new Throwable().getStackTrace());
            Log.i(TAG + "_" + tag, printLog(message));
        }
    }

    public static void d(String message) {
        if (debug) {
            getMethodNames(new Throwable().getStackTrace());
            Log.d(TAG, printLog(message));
        }
    }

    public static void d(String tag, String message) {
        if (debug) {
            getMethodNames(new Throwable().getStackTrace());
            Log.d(TAG + "_" + tag, printLog(message));
        }
    }

    public static void v(String message) {
        if (debug) {
            getMethodNames(new Throwable().getStackTrace());
            Log.v(TAG, printLog(message));
        }
    }

    public static void v(String tag, String message) {
        if (debug) {
            getMethodNames(new Throwable().getStackTrace());
            Log.v(TAG + "_" + tag, printLog(message));
        }
    }

    public static void w(String message) {
        if (debug) {
            getMethodNames(new Throwable().getStackTrace());
            Log.w(TAG, printLog(message));
        }
    }

    public static void w(String tag, String message) {
        if (debug) {
            getMethodNames(new Throwable().getStackTrace());
            Log.w(TAG + "_" + tag, printLog(message));
        }
    }

    public static void wtf(String message) {
        if (debug) {
            getMethodNames(new Throwable().getStackTrace());
            Log.wtf(TAG, printLog(message));
        }
    }


}