Encapsulation of log printing class in Android

In general, we will use log many times in development, but when we launch the application, if our log is still there, some information will be leaked. Therefore, we usually delete or comment out these codes before going online. But this increased our workload and it was not convenient for us to maintain the project.

So we try to encapsulate the Log that comes with the system to achieve our one-click configuration of whether or not to print the Log.

The Log tool class is packaged as follows

public class LogTool {
    public static boolean isShown = false;  // false表示上线模式,true表示开发模式

    public static void v(String tag, String msg) {
        if (isShown) {
            Log.v(tag, msg); // 打印冗余日志
        }
    }

    public static void d(String tag, String msg) {
        if (isShown) {
            Log.d(tag, msg); // 打印调试日志
        }
    }

    public static void i(String tag, String msg) {
        if (isShown) {
            Log.i(tag, msg); // 打印一般日志
        }
    }

    public static void w(String tag, String msg) {
        if (isShown) {
            Log.w(tag, msg); // 打印警告日志
        }
    }

    public static void e(String tag, String msg) {
        if (isShown) {
            Log.e(tag, msg); // 打印错误日志
        }
    }

    public static void wtf(String tag, String msg) {
        if (isShown) {
            Log.wtf(tag, msg);
        }
    }
}

In this way, when isShown is true, it is in the debugging state, and our log will be printed. If it is false, the log in the application will not be printed.

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/115028247