Log日志工具类(一)

实现的效果能将json字符串以格式化的方式显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* $desc$ Log日志工具类
*
* @Author mark
* @Date 2018/11/26
*/
public class LogUtils {
//是否Debug
public static final boolean isMarkDebug = true;

public static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static void printLog(String name,String strContent){
if (isMarkDebug){
Log.d(name,strContent);
}
}
public static void printLine(String tag, boolean isTop) {
if (isTop) {
Log.d(tag, "╔═══════════════════════════════════════════════════════════════════════════════════════");
} else {
Log.d(tag, "╚═══════════════════════════════════════════════════════════════════════════════════════");
}
}
public static void printJson(String tag, String msg, String headString) {

String message;

try {
if (msg.startsWith("{")) {
JSONObject jsonObject = new JSONObject(msg);
message = jsonObject.toString(4);//最重要的方法,就一行,返回格式化的json字符串,其中的数字4是缩进字符数
} else if (msg.startsWith("[")) {
JSONArray jsonArray = new JSONArray(msg);
message = jsonArray.toString(4);
} else {
message = msg;
}
} catch (JSONException e) {
message = msg;
}

printLine(tag, true);
message = headString + LINE_SEPARATOR + message;
String[] lines = message.split(LINE_SEPARATOR);
for (String line : lines) {
Log.d(tag, "║ " + line);
}
printLine(tag, false);
}
}