溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

一個(gè)在用的Log日志工具

發(fā)布時(shí)間:2020-08-02 03:44:21 來(lái)源:網(wǎng)絡(luò) 閱讀:239 作者:chuyuan 欄目:移動(dòng)開(kāi)發(fā)

import android.util.Log;

/**

 * 

 * 日志工具

 *

 */

public class LogUtil

{

    private static final boolean WRITE_LOG = true;

    

    private static boolean hasLogFile = false;

    

    private static String TAG = "LogUtil";

    

    private LogUtil()

    {

    }

    

    @SuppressWarnings("unused")

    public static void v(String tag, String msg)

    {

        if (WRITE_LOG || hasLogFile)

        {

            String className = "";

            String logInfo = "";

            try

            {

                String fullClassName = Thread.currentThread().getStackTrace()[3].getClassName();

                // 獲取打印日志所在的類名

                className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);

                // 獲取打印日志所在的方法名

                String methodName = Thread.currentThread().getStackTrace()[3].getMethodName();

                // 獲取日志所在的行號(hào)

                int lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();

                // 日志信息。包括了 方法名字 行數(shù) 時(shí)間 以及要打印的信息

                logInfo = className + ":" + methodName + "() line(" + lineNumber + "):" + msg;

                

            }

            catch (Exception e)

            {

                className = tag;

                logInfo = msg;

                Log.e(TAG, "log error");

            }

            

            if (tag == null || "".equals(tag))

            {

                tag = className;

            }

            

            Log.v(tag, logInfo);

        }

    }

    

    @SuppressWarnings("unused")

    public static void v(String tag, String msg, Throwable tr)

    {

        if (WRITE_LOG || hasLogFile)

        {

            String className = "";

            String logInfo = "";

            try

            {

                String fullClassName = Thread.currentThread().getStackTrace()[3].getClassName();

                // 獲取打印日志所在的類名

                className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);

                // 獲取打印日志所在的方法名

                String methodName = Thread.currentThread().getStackTrace()[3].getMethodName();

                // 獲取日志所在的行號(hào)

                int lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();

                // 日志信息。包括了 方法名字 行數(shù) 時(shí)間 以及要打印的信息

                logInfo = className + ":" + methodName + "() line(" + lineNumber + "):" + msg;

                

            }

            catch (Exception e)

            {

                className = tag;

                logInfo = msg;

                Log.e(TAG, "log error");

            }

            

            if (tag == null || "".equals(tag))

            {

                tag = className;

            }

            

            Log.v(tag, logInfo, tr);

        }

    }

    

    @SuppressWarnings("unused")

    public static void d(String tag, String msg)

    {

        if (WRITE_LOG || hasLogFile)

        {

            String className = "";

            String logInfo = "";

            try

            {

                String fullClassName = Thread.currentThread().getStackTrace()[3].getClassName();

                // 獲取打印日志所在的類名

                className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);

                // 獲取打印日志所在的方法名

                String methodName = Thread.currentThread().getStackTrace()[3].getMethodName();

                // 獲取日志所在的行號(hào)

                int lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();

                // 日志信息。包括了 方法名字 行數(shù) 時(shí)間 以及要打印的信息

                logInfo = className + ":" + methodName + "() line(" + lineNumber + "):" + msg;

                

            }

            catch (Exception e)

            {

                className = tag;

                logInfo = msg;

                Log.e(TAG, "log error");

            }

            

            if (tag == null || "".equals(tag))

            {

                tag = className;

            }

            

            Log.d(tag, logInfo);

        }

    }

    

    @SuppressWarnings("unused")

    public static void d(String tag, String msg, Throwable tr)

    {

        if (WRITE_LOG || hasLogFile)

        {

            String className = "";

            String logInfo = "";

            String methodName = "";

            int lineNumber = 0;

            try

            {

                String fullClassName = Thread.currentThread().getStackTrace()[3].getClassName();

                // 獲取打印日志所在的類名

                className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);

                // 獲取打印日志所在的方法名

                methodName = Thread.currentThread().getStackTrace()[3].getMethodName();

                // 獲取日志所在的行號(hào)

                lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();

                // 日志信息。包括了 方法名字 行數(shù) 時(shí)間 以及要打印的信息

                logInfo = className + ":" + methodName + "() line(" + lineNumber + "):" + msg;

                

            }

            catch (Exception e)

            {

                className = tag;

                logInfo = msg;

                Log.e(TAG, "log error");

            }

            

            if (tag == null || "".equals(tag))

            {

                tag = className;

            }

            

            Log.d(tag, logInfo, tr);

        }

    }

    

    @SuppressWarnings("unused")

    public static void i(String tag, String msg)

    {

        if (WRITE_LOG || hasLogFile)

        {

            String className = "";

            String logInfo = "";

            String methodName = "";

            int lineNumber = 0;

            try

            {

                String fullClassName = Thread.currentThread().getStackTrace()[3].getClassName();

                // 獲取打印日志所在的類名

                className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);

                // 獲取打印日志所在的方法名

                methodName = Thread.currentThread().getStackTrace()[3].getMethodName();

                // 獲取日志所在的行號(hào)

                lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();

                // 日志信息。包括了 方法名字 行數(shù) 時(shí)間 以及要打印的信息

                logInfo = className + ":" + methodName + "() line(" + lineNumber + "):" + msg;

                

            }

            catch (Exception e)

            {

                className = tag;

                logInfo = msg;

                Log.e(TAG, "log error");

            }

            

            if (tag == null || "".equals(tag))

            {

                tag = className;

            }

            

            Log.i(tag, logInfo);

        }

    }

    

    @SuppressWarnings("unused")

    public static void i(String tag, String msg, Throwable tr)

    {

        if (WRITE_LOG || hasLogFile)

        {

            String className = "";

            String logInfo = "";

            try

            {

                String fullClassName = Thread.currentThread().getStackTrace()[3].getClassName();

                // 獲取打印日志所在的類名

                className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);

                // 獲取打印日志所在的方法名

                String methodName = Thread.currentThread().getStackTrace()[3].getMethodName();

                // 獲取日志所在的行號(hào)

                int lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();

                // 日志信息。包括了 方法名字 行數(shù) 時(shí)間 以及要打印的信息

                logInfo = className + ":" + methodName + "() line(" + lineNumber + "):" + msg;

                

            }

            catch (Exception e)

            {

                className = tag;

                logInfo = msg;

                Log.e(TAG, "log error");

            }

            

            if (tag == null || "".equals(tag))

            {

                tag = className;

            }

            

            Log.i(tag, logInfo, tr);

        }

    }

    

    public static void e(String tag, String msg, Throwable tr)

    {

        String className = "";

        String logInfo = "";

        try

        {

            String fullClassName = Thread.currentThread().getStackTrace()[3].getClassName();

            // 獲取打印日志所在的類名

            className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);

            // 獲取打印日志所在的方法名

            String methodName = Thread.currentThread().getStackTrace()[3].getMethodName();

            // 獲取日志所在的行號(hào)

            int lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();

            // 日志信息。包括了 方法名字 行數(shù) 時(shí)間 以及要打印的信息

            logInfo = className + ":" + methodName + "() line(" + lineNumber + "):" + msg;

            

        }

        catch (Exception e)

        {

            className = tag;

            logInfo = msg;

            Log.e(TAG, "log error");

        }

        

        if (tag == null || "".equals(tag))

        {

            tag = className;

        }

        

        Log.e(tag, logInfo, tr);

    }

    

    public static void e(String tag, String msg)

    {

        String className = "";

        String logInfo = "";

        try

        {

            String fullClassName = Thread.currentThread().getStackTrace()[3].getClassName();

            // 獲取打印日志所在的類名

            className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);

            // 獲取打印日志所在的方法名

            String methodName = Thread.currentThread().getStackTrace()[3].getMethodName();

            // 獲取日志所在的行號(hào)

            int lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();

            // 日志信息。包括了 方法名字 行數(shù) 時(shí)間 以及要打印的信息

            logInfo = className + ":" + methodName + "() line(" + lineNumber + "):" + msg;

            

        }

        catch (Exception e)

        {

            className = tag;

            logInfo = msg;

            Log.e(TAG, "log error");

        }

        

        if (tag == null || "".equals(tag))

        {

            tag = className;

        }

        

        Log.e(tag, logInfo);

    }

    

    @SuppressWarnings("unused")

    public static void w(String tag, String msg)

    {

        if (WRITE_LOG || hasLogFile)

        {

            String className = "";

            String logInfo = "";

            try

            {

                String fullClassName = Thread.currentThread().getStackTrace()[3].getClassName();

                // 獲取打印日志所在的類名

                className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);

                // 獲取打印日志所在的方法名

                String methodName = Thread.currentThread().getStackTrace()[3].getMethodName();

                // 獲取日志所在的行號(hào)

                int lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();

                // 日志信息。包括了 方法名字 行數(shù) 時(shí)間 以及要打印的信息

                logInfo = className + ":" + methodName + "() line(" + lineNumber + "):" + msg;

                

            }

            catch (Exception e)

            {

                className = tag;

                logInfo = msg;

                Log.e(TAG, "log error");

            }

            

            if (tag == null || "".equals(tag))

            {

                tag = className;

            }

            

            Log.w(tag, logInfo);

        }

    }

    

    @SuppressWarnings("unused")

    public static void w(String tag, String msg, Throwable tr)

    {

        if (WRITE_LOG || hasLogFile)

        {

            String className = "";

            String logInfo = "";

            try

            {

                String fullClassName = Thread.currentThread().getStackTrace()[3].getClassName();

                // 獲取打印日志所在的類名

                className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);

                // 獲取打印日志所在的方法名

                String methodName = Thread.currentThread().getStackTrace()[3].getMethodName();

                // 獲取日志所在的行號(hào)

                int lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();

                // 日志信息。包括了 方法名字 行數(shù) 時(shí)間 以及要打印的信息

                logInfo = className + ":" + methodName + "() line(" + lineNumber + "):" + msg;

                

            }

            catch (Exception e)

            {

                className = tag;

                logInfo = msg;

                Log.e(TAG, "log error");

            }

            

            if (tag == null || "".equals(tag))

            {

                tag = className;

            }

            

            Log.w(tag, logInfo, tr);

        }

    }

}


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI