Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Switch to a log printing class

I recently looked at other people’s code and found that there is a very good log printing class, which can not only see the current log thread, but also locate the log on the line, which is convenient. Let’s see which library it is.

My logging class

I use the following logging tool class, which has been with me for at least five years, copying it every time a new project comes along.

The only exception is that buildconfig. DEBUG determines the environment of the development project, so logs can be automatically turned off in the formal environment and exported in the test environment.

So this logging tool class is not very exciting, then look at other people’s logging class

Public class LogWrapper {/** * public static final Boolean DEBUG = buildconfig.debug; // private static final boolean DEBUG = true; private static final String TAG = "LogWrapper"; public static void v(String tag, String msg) { if (DEBUG) Log.v(tag, msg); } public static void d( String msg) { if (DEBUG) Log.d(TAG, msg); } public static void d(String tag, String msg) { if (DEBUG) Log.d(tag, msg); } public static void i(String tag, String msg) { if (DEBUG) Log.i(tag, msg); } public static void w(String tag, String msg) { if (DEBUG) Log.w(tag, msg); } public static void e(String tag, String msg) { if (DEBUG) Log.e(tag, msg); } public static void e(String msg) { if (DEBUG) Log.e(TAG, msg); } public static void w(String tag, String msg, Throwable ex) { if (DEBUG) Log.w(tag, msg, ex); } public static void e(String tag, String msg, Throwable ex) { if (DEBUG) Log.e(tag, msg, ex); }}Copy the code

Someone else’s logging class

Look at the map

See? It’s fancy, but it can be obvious and useful.

  1. To determine the thread of the log, it is very easy to see whether the current log is in the main thread or child thread. If it is a child thread, we cannot update the interface.

  2. Locate the log printing position. Is there something out there that prints logs that don’t make any sense, but we don’t know where the logs came from, and this library can jump to the corresponding location by clicking the link in the blue section.

  3. Json/XML supports output. Gson is usually converted to a string, so this library can just print it out. One more step.

  4. Support for interceptor operations. Filter logs in simple steps.

Its name is Logger, and there is already a 13K Star on Github, please use it.

logger