LogUtil is often used for Android development. logutil is developed for android.
Log is often used during development. One of the methods we often write is to customize a LogUtil tool class.
private static boolean LOGV = true; private static boolean LOGD = true; private static boolean LOGI = true; private static boolean LOGW = true; private static boolean LOGE = true; public static void v(String tag, String mess) { if (LOGV) { Log.v(tag, mess); } } public static void d(String tag, String mess) { if (LOGD) { Log.d(tag, mess); } } public static void i(String tag, String mess) { if (LOGI) { Log.i(tag, mess); } } public static void w(String tag, String mess) { if (LOGW) { Log.w(tag, mess); } } public static void e(String tag, String mess) { if (LOGE) { Log.e(tag, mess); } }
This tool can classify logs and disable logs when releasing applications, which is more convenient.
However, the problem is that, similar to using Log directly, a TAG needs to be defined. A common method is to define a static String constant TAG for each log class and assign a value to the class name. If you forget to modify the TAG during refactoring, you may be confused when viewing the log. Of course, there are also a lot of people who will map it easily and directly use System. out. println (str); for output.
When I read VolleyLog two days ago, I learned a new method,
private static String getTag() { StackTraceElement[] trace = new Throwable().fillInStackTrace() .getStackTrace(); String callingClass = ""; for (int i = 2; i < trace.length; i++) { Class<?> clazz = trace[i].getClass(); if (!clazz.equals(LogUtil.class)) { callingClass = trace[i].getClassName(); callingClass = callingClass.substring(callingClass .lastIndexOf('.') + 1); break; } } return callingClass; }
This method can directly obtain the caller's class name. Use:
public static void v(String mess) { if (LOGV) { Log.v(getTag(), mess); } } public static void d(String mess) { if (LOGD) { Log.d(getTag(), mess); } } public static void i(String mess) { if (LOGI) { Log.i(getTag(), mess); } } public static void w(String mess) { if (LOGW) { Log.w(getTag(), mess); } } public static void e(String mess) { if (LOGE) { Log.e(getTag(), mess); } }
In this case, you do not need to define tags in the class.
When debugging a program, we often print some information, including the method name/row number. The following method can save the trouble:
private static String buildMessage(String msg) { StackTraceElement[] trace = new Throwable().fillInStackTrace() .getStackTrace(); String caller = ""; for (int i = 2; i < trace.length; i++) { Class<?> clazz = trace[i].getClass(); if (!clazz.equals(LogUtil.class)) { caller = trace[i].getMethodName(); break; } } return String.format(Locale.US, "[%d] %s: %s", Thread.currentThread() .getId(), caller, msg); }
The usage is as follows:
public static void v(String mess) { if (LOGV) { Log.v(getTag(), buildMessage(mess)); } } public static void d(String mess) { if (LOGD) { Log.d(getTag(), buildMessage(mess)); } } public static void i(String mess) { if (LOGI) { Log.i(getTag(), buildMessage(mess)); } } public static void w(String mess) { if (LOGW) { Log.w(getTag(), buildMessage(mess)); } } public static void e(String mess) { if (LOGE) { Log.e(getTag(), buildMessage(mess)); } }
In this way, it is very convenient to print logs each time. You can directly type: LogUtil. v (msg );
TAG, method name, thread ID, and other information are not required
Another optimization is to format the msg content.
buildMessage(String format, Object... args)
Finally, it is explained that if a large number of logs are played in this way, the program performance will be affected, so this method is only convenient for debugging. During the release, you can disable the debug log.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.