Android打印日志到文件

项目遇到一个问题需要排查,由于是偶现的问题,所以需要打印特定的日志并保存下来,但是并不需要将所有的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
public class LogcatHelper {
private static final String FOLDER_NAME = "TestAppName";
private static final String FILE_NAME = "TestAppName";
private static LogcatHelper INSTANCE = null;
private static String PATH_LOGCAT;
private LogDumper mLogDumper = null;
private int mPId;

/**
* 初始化目录
*/
private void init(Context context) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {// 优先保存到SD卡中
PATH_LOGCAT = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + FOLDER_NAME;
} else {// 如果SD卡不存在,就保存到本应用的目录下
PATH_LOGCAT = context.getFilesDir().getAbsolutePath()
+ File.separator + FOLDER_NAME;
}
File file = new File(PATH_LOGCAT);
if (!file.exists()) {
file.mkdirs();
}
}

public static LogcatHelper getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new LogcatHelper(context);
}
return INSTANCE;
}

private LogcatHelper(Context context) {
init(context);
mPId = android.os.Process.myPid();
}

public void start() {
if (mLogDumper == null)
mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT);
mLogDumper.start();
}

public void stop() {
if (mLogDumper != null) {
mLogDumper.stopLogs();
mLogDumper = null;
}
}

private class LogDumper extends Thread {
private FileWriter writer;

public LogDumper(String pid, String dir) {
try {
writer = new FileWriter(new File(dir, FILE_NAME + "-" + MyDate.getFileName() + ".log"), true);
} catch (FileNotFoundException e) {
e.printStackTrace();
Timber.e(e);
} catch (IOException e) {
e.printStackTrace();
}
}

public void stopLogs() {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}

@SuppressLint("CheckResult")
@Override
public void run() {
RxBus.register(LogData.class)
.subscribe(re -> {
if (writer != null) {
writer.write(re.getData() + "\n");
writer.flush();
}
}, ErrorHandlers.high()::handle);
}
}


public static class MyDate {
public static String getFileName() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date = format.format(new Date(System.currentTimeMillis()));
return date;// 2012年10月03日 23:41:31
}

public static String getDateEN() {
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
String date1 = format1.format(new Date(System.currentTimeMillis()));
return date1;// 2012-10-03 23:41:31
}

}

public static class LogData {

private String data;

public LogData(String data) {
this.data = MyDate.getDateEN() + " " + data;
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}

}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×