常用打印Log的工具集合
格式化log輸出排作,主tag膀值,子tag,線程名畦粮,類及方法露泊,文件及行數(shù)喉镰,最后是自定義的message。
格式化json字符串輸出(Gson)惭笑。
行數(shù)(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
中實現(xiàn)了一個默認的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
實現(xiàn)了一個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);
}
}