常用打印Log的工具集合
格式化log输出,主tag,子tag,线程名,类及方法,文件及行数,最后是自定义的message。
格式化json字符串输出(Gson)。
行数(MainActivity.java:14)
方便查看打印位置,并且在AndroidStudio中可以直接通过点击定位到代码位置。
示例:
|LogUtilsDemo_TAG1|main|: MainActivity.onCreate(MainActivity.java:14) message1
|LogUtilsDemo_TAG2|main|: MainActivity.onCreate(MainActivity.java:15) message2
LogUtils
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.HashMap;
import java.util.Locale;
public class LogUtils {
private static String APP_TAG = "LogUtils";
private static HashMap<String, String> sCachedTag = new HashMap<>();
private static JsonFormatter sJsonFormatter = new DefaultFormatter();
private LogUtils() {
throw new UnsupportedOperationException(this.getClass().getSimpleName() + " cannot be instantiated");
}
public static void Init(@NonNull String appTag, @NonNull JsonFormatter formatter) {
APP_TAG = appTag;
sJsonFormatter = formatter;
}
public static void i(String message) {
Log.i(buildTag(APP_TAG), buildMessage(message));
}
public static void d(String message) {
Log.d(buildTag(APP_TAG), buildMessage(message));
}
public static void w(String message) {
Log.w(buildTag(APP_TAG), buildMessage(message));
}
public static void e(String message) {
Log.e(buildTag(APP_TAG), buildMessage(message));
}
public static void v(String message) {
Log.v(buildTag(APP_TAG), buildMessage(message));
}
public static void wtf(String message) {
Log.wtf(buildTag(APP_TAG), buildMessage(message));
}
public static void json(String message) {
Log.v(buildTag(APP_TAG), buildMessage(message));
}
public static void i(@NonNull String tag, String message) {
Log.i(buildTag(tag), buildMessage(message));
}
public static void d(@NonNull String tag, String message) {
Log.d(buildTag(tag), buildMessage(message));
}
public static void w(@NonNull String tag, String message) {
Log.w(buildTag(tag), buildMessage(message));
}
public static void e(@NonNull String tag, String message) {
Log.e(buildTag(tag), buildMessage(message));
}
public static void v(@NonNull String tag, String message) {
Log.v(buildTag(tag), buildMessage(message));
}
public static void wtf(@NonNull String tag, String message) {
Log.wtf(buildTag(tag), buildMessage(message));
}
public static void json(@NonNull String tag, String content) {
Log.v(buildTag(tag), buildMessage(formatJson(content)));
}
private static String buildTag(@NonNull String tag) {
String key = String.format(Locale.US, "%s@%s", tag, Thread.currentThread().getName());
if (!sCachedTag.containsKey(key)) {
if (APP_TAG.equals(tag)) {
sCachedTag.put(key, String.format(Locale.US, "|%s|%s|",
tag,
Thread.currentThread().getName()
));
} else {
sCachedTag.put(key, String.format(Locale.US, "|%s_%s|%s|",
APP_TAG,
tag,
Thread.currentThread().getName()
));
}
}
return sCachedTag.get(key);
}
private static String buildMessage(String message) {
StackTraceElement[] traceElements = Thread.currentThread().getStackTrace();
if (traceElements == null || traceElements.length < 4) {
return message;
}
StackTraceElement traceElement = traceElements[4];
return String.format(Locale.US, "%s.%s(%s:%d) %s",
traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1),
traceElement.getMethodName(),
traceElement.getFileName(),
traceElement.getLineNumber(),
message
);
}
private static String formatJson(String content) {
return String.format(Locale.US, "\n%s", sJsonFormatter.formatJson(content));
}
public interface JsonFormatter {
String formatJson(String content);
}
private static class DefaultFormatter implements JsonFormatter {
@Override
public String formatJson(String content) {
return content;
}
}
}
为了减少工具的依赖,降低耦合,支持自定义JsonFormatter
,通过Init()
方法注入。LogUtils
中实现了一个默认的Formatter
,但并未对json
字符串做任何处理。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LogUtils.Init("LogUtilsDemo", new GsonFormatter());
LogUtils.i("TAG1", "onCreate: 1");
LogUtils.i("TAG2", "onCreate: 2");
LogUtils.i("TAG2", "onCreate: 3");
LogUtils.i("TAG1", "onCreate: 4");
LogUtils.i("TAG3", "onCreate: 5");
LogUtils.i("TAG1", "onCreate: 6");
LogUtils.i("LogUtilsDemo", "onCreate: 7");
LogUtils.json("TAG1", "{\"age\":23,\"hobby\":[\"reading,writing\",\"travel\"],\"name\":{\"firstName\":\"zhang\",\"lastName\":\"san\"}} ");
LogUtils.i("", "onCreate: 8");
LogUtils.i(null, "onCreate: 9-1");
LogUtils.i("null", "onCreate: 9-2");
LogUtils.i(null, "onCreate: 9-3");
LogUtils.i(null, null);
LogUtils.i("onCreate: 10");
LogUtils.i(null);
}
Log输出样式:
I/|LogUtilsDemo_TAG1|main|: MainActivity.onCreate(MainActivity.java:15) onCreate: 1
I/|LogUtilsDemo_TAG2|main|: MainActivity.onCreate(MainActivity.java:16) onCreate: 2
I/|LogUtilsDemo_TAG2|main|: MainActivity.onCreate(MainActivity.java:17) onCreate: 3
I/|LogUtilsDemo_TAG1|main|: MainActivity.onCreate(MainActivity.java:18) onCreate: 4
I/|LogUtilsDemo_TAG3|main|: MainActivity.onCreate(MainActivity.java:19) onCreate: 5
I/|LogUtilsDemo_TAG1|main|: MainActivity.onCreate(MainActivity.java:20) onCreate: 6
I/|LogUtilsDemo|main|: MainActivity.onCreate(MainActivity.java:21) onCreate: 7
V/|LogUtilsDemo_TAG1|main|: MainActivity.onCreate(MainActivity.java:22)
{
"age": 23,
"hobby": [
"reading,writing",
"travel"
],
"name": {
"firstName": "zhang",
"lastName": "san"
}
}
I/|LogUtilsDemo_|main|: MainActivity.onCreate(MainActivity.java:23) onCreate: 8
I/|LogUtilsDemo_null|main|: MainActivity.onCreate(MainActivity.java:24) onCreate: 9-1
I/|LogUtilsDemo_null|main|: MainActivity.onCreate(MainActivity.java:25) onCreate: 9-2
I/|LogUtilsDemo_null|main|: MainActivity.onCreate(MainActivity.java:26) onCreate: 9-3
I/|LogUtilsDemo_null|main|: MainActivity.onCreate(MainActivity.java:27) null
I/|LogUtilsDemo|main|: MainActivity.onCreate(MainActivity.java:28) onCreate: 10
I/|LogUtilsDemo|main|: MainActivity.onCreate(MainActivity.java:29) null
测试代码中使用Gson
实现了一个Formatter
:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
public class GsonFormatter implements LogUtils.JsonFormatter {
@Override
public String formatJson(String content) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(content);
return gson.toJson(element);
}
}