Encapsulation of Android logstore Logger and the problem of AS3.0 log not being aligned

Keywords: xml JSON Android github

It doesn't matter how to introduce loggers. There are a lot of them on the Internet. This article records the encapsulation of loggers in use and the use of loggers in Android Studio 3.0 and above.

Encapsulation of Logger

When using any third-party library, if you can encapsulate it once to replace it later, otherwise you need to modify all the places in the whole project where the third-party library is used.

The specific code is as follows:

public final class LogUtils {

    /**
     * Here, you can use the Logger.t() method to set the tag, or you can configure the global tag during the initialization of the Logger
     */
    private static final String TAG = "TAG";

    //Print or not: true by default
    public static final boolean IS_PRINT_LOG = true;

    /**
     * Another way to control whether or not to print logs. Note that the imported package is: import com.orhanobout.logger.buildconfig
     * The advantage of this method is that the debug and release versions do not need to manually modify the log and will automatically print or not print. The disadvantage is that if there are multiple module s, it will lead to the problem of unable to print. Specifically solve the problem of Baidu
     */
//    public static final boolean IS_PRINT_LOG = BuildConfig.DEBUG;


    public static void v(String tag, String message) {
        if (IS_PRINT_LOG) {
            Logger.t(tag).v(message);
        }
    }

    public static void v(String message) {
        if (IS_PRINT_LOG) {
            Logger.t(TAG).v(message);
        }
    }

    public static void d(String tag, Object message) {
        if (IS_PRINT_LOG) {
            Logger.t(tag).d(message);
        }
    }

    public static void d(Object message) {
        if (IS_PRINT_LOG) {
            Logger.d(message);
        }
    }

    public static void i(String tag, String message) {
        if (IS_PRINT_LOG) {
            Logger.t(tag).i(message);
        }
    }

    public static void i(String message) {
        if (IS_PRINT_LOG) {
            Logger.t(TAG).i(message);
        }
    }

    public static void w(String tag, String message) {
        if (IS_PRINT_LOG) {
            Logger.t(tag).w(message);
        }
    }

    public static void w(String message) {
        if (IS_PRINT_LOG) {
            Logger.t(TAG).w(message);
        }
    }

    public static void json(String tag, String json) {
        if (IS_PRINT_LOG) {
            Logger.t(tag).json(json);
        }
    }

    public static void json(String json) {
        if (IS_PRINT_LOG) {
            Logger.t(TAG).json(json);
        }
    }

    public static void e(String tag, String message) {
        if (IS_PRINT_LOG) {
            Logger.t(tag).e(message);
        }
    }

    public static void e(String message) {
        if (IS_PRINT_LOG) {
            Logger.t(TAG).e(message);
        }
    }

    public static void e(Exception e, String message) {
        if (IS_PRINT_LOG) {
            Logger.e(e, message);
        }
    }

    public static void xml(String xml) {
        if (IS_PRINT_LOG) {
            Logger.t(TAG).xml(xml);
        }
    }

    public static void xml(String tag, String xml) {
        if (IS_PRINT_LOG) {
            Logger.t(tag).xml(xml);
        }
    }
}

Solve the problem that the Logger cannot be aligned in Android Studio 3.0 and above

After Android Studio is updated to 3.0, it is found that the printed logs will wrap automatically, resulting in the phenomenon that they cannot be aligned, as shown in the following figure:

The picture is taken from the problem submitted by github. My problem has been solved, so I haven't set it back.

There is a solution in the Issues of the Logger library, as shown in the figure below:

Or directly list the solutions here. The code is as follows:

public class LogCatStrategy implements LogStrategy {

        @Override
        public void log(int priority, String tag, String message) {
            Log.println(priority, randomKey() + tag, message);
        }

        private int last;

        private String randomKey() {
            int random = (int) (10 * Math.random());
            if (random == last) {
                random = (random + 1) % 10;
            }
            last = random;
            return String.valueOf(random);
        }
    }  


//When the Logger is initialized, instantiate the new class above and set it to the Logger
 PrettyFormatStrategy strategy = PrettyFormatStrategy.newBuilder()
                .logStrategy(new LogCatStrategy())
                .tag("TAG")//(optional) global TAG can also be set here
                .build();
 Logger.addLogAdapter(new AndroidLogAdapter(strategy));

 //It can also be written as follows to control whether the log is printed
 Logger.addLogAdapter(new AndroidLogAdapter(strategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return BuildConfig.DEBUG;//(control whether the log is printed)
            }
        });

Posted by yuan22m on Sat, 04 Apr 2020 12:10:11 -0700