Crash抓取App全局log保存到本地

代码

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* created by JesseHev
*/
public class CrashHandler implements Thread.UncaughtExceptionHandler {
public static final String TAG = "CrashHandler";
// 系统默认的UncaughtException处理类
private Thread.UncaughtExceptionHandler mDefaultHandler;
// CrashHandler实例
private static CrashHandler INSTANCE = new CrashHandler();
public static String LOG_PATH = "/jesse/log";
private Map<String, String> mInfos = new HashMap<>();
private Context mContext;
private CrashHandler() {
}
public static CrashHandler getInstance() {
return INSTANCE;
}
public void init(Context context) {
mContext = context;
// 获取系统默认的UncaughtException处理器
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
// 设置该CrashHandler为程序的默认处理器
Thread.setDefaultUncaughtExceptionHandler(this);
}
/**
* 当UncaughtException发生时会转入该函数来处理
*/
@Override public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mDefaultHandler != null) {
// 如果用户没有处理则让系统默认的异常处理器来处理
mDefaultHandler.uncaughtException(thread, ex);
} else {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Log.e(TAG, "error : ", e);
}
// 退出程序
System.exit(10);
}
}
/**
* 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
*
* @return true:如果处理了该异常信息;否则返回false.
*/
private boolean handleException(Throwable ex) {
if (ex == null) {
return false;
}
new Thread() {
@Override public void run() {
Looper.prepare();
Looper.loop();
}
}.start();
//获取设备信息
getDeviceInfo();
// 保存日志文件
saveCrashInfo(ex);
return true;
}
/**
* 保存错误信息到文件中
*/
private void saveCrashInfo(Throwable ex) {
StringBuilder sb = new StringBuilder();
sb.append(printDeviceInfo());
sb.append(getExceptionString(ex));
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String path = getLogPath();
FileOutputStream fos = new FileOutputStream(path);
fos.write(sb.toString().getBytes());
fos.close();
}
} catch (Exception e) {
}
}
/**
* 将异常信息转换为字符串
*/
public static String getExceptionString(Throwable ex) {
StringBuilder err = new StringBuilder();
err.append("ExceptionDetailed:\n");
err.append("====================Exception Info====================\n");
err.append(ex.toString());
err.append("\n");
StackTraceElement[] stack = ex.getStackTrace();
for (StackTraceElement stackTraceElement : stack) {
err.append(stackTraceElement.toString()).append("\n");
}
Throwable cause = ex.getCause();
if (cause != null) {
err.append("【Caused by】: ");
err.append(cause.toString());
err.append("\n");
StackTraceElement[] stackTrace = cause.getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
err.append(stackTraceElement.toString()).append("\n");
}
}
return err.toString();
}
/**
* 获取log保存目录
*/
public static String getLogPath() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String time = dateFormat.format(new Date());
String path = android.os.Environment.getExternalStorageDirectory().getPath()
//+ File.separator
+ LOG_PATH + "-" + time + ".txt";
File log = new File(path);
if (!log.getParentFile().exists()) {
log.getParentFile().mkdirs();
}
if (!log.exists()) {
try {
log.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return path;
}
/**
* 收集设备参数信息
*/
public String printDeviceInfo() {
StringBuilder sb = new StringBuilder();
sb.append("====================Device Info====================\n");
for (Map.Entry<String, String> entry : mInfos.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
sb.append(key + "=" + value + "\n");
}
sb.append("=================================================\n");
return sb.toString();
}
/**
* 通过反射获取设备信息
*/
private void getDeviceInfo() {
Field[] fields = Build.class.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
mInfos.put(field.getName(), field.get(null).toString());
} catch (Exception e) {
}
}
}
}