Log printing class -- LogUtils

In Android development, it is often necessary to print logs, but the system logs, first, need to write tags frequently, but tags often use the same, second, can't output the number of lines, third, the logs that may be printed are very long and can't be printed completely, so a simple log printing class is needed to write less code. Upper Code:

import android.util.Log;

/**
 * Created by more on 2017/12/7/007.
 */

public class LogUtils {

    public static void setDebugMode(boolean debugMode) {
        LogUtils.debugMode = debugMode;
    }

    public static boolean isDebugMode() {
        return debugMode;
    }

    static boolean debugMode = true;

  
    static String className;//Class name
    static String methodName;//Method name
    static int lineNumber;//Row number


    private static String createLog(String log) {

        StringBuffer buffer = new StringBuffer();

        buffer.append(methodName);

        buffer.append("(").append(className).append(":").append(lineNumber).append(")");

        return buffer.toString();
    }

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


    public static void logMe(String message) {


        if (debugMode) {

            Log.e("666", "|                                         |");

            logVeryLongLoge("666", "|" + message);

            getMethodNames(new Throwable().getStackTrace());
            Log.e("666", "|" + createLog(message));
            Log.e("666", "|                                         |");
            Log.e("666", "-----------------------------------------------------------------------");
            Log.e("666", "|                                         |");
        }
    }

   



    public static void logMe999(String message) {


        if (debugMode) {

            Log.e("999", "|                                         |");
            Log.e("999", "|" + message);
            getMethodNames(new Throwable().getStackTrace());
            Log.e("999", "|" + createLog(message));
            Log.e("999", "|                                         |");
            Log.e("999", "-----------------------------------------------------------------------");
            Log.e("999", "|                                         |");
        }
    }


   

    /**
     * Truncate output log
     * @param msg
     */
    public static void logVeryLongLoge(String tag, String msg) {
        if (tag == null || tag.length() == 0
                || msg == null || msg.length() == 0)
            return;

        int segmentSize = 3 * 1024;
        long length = msg.length();
        if (length <= segmentSize ) {// Length less than or equal to limit direct printing
            Log.e(tag, msg);
        }else {
            while (msg.length() > segmentSize ) {// Cycle segment print log
                String logContent = msg.substring(0, segmentSize );
                msg = msg.replace(logContent, "");
                Log.e(tag, logContent);
            }
            Log.e(tag, msg);// Print remaining logs
        }
    }

}

 

 

Use:

LogUtils.logMe("filePath :" + filePath);

 

Reference documents:

  1. Solutions for incomplete log printing of Logcat in Android https://www.jianshu.com/p/9fcdda2d6b7d
  2. Super Log tool, which can display your file name, method, number of lines and click to the line https://blog.csdn.net/wenzhi20102321/article/details/78138399

Keywords: Android less

Added by php_dev_101 on Mon, 23 Dec 2019 00:25:05 +0200