CsvFormatStrategy.java
4.37 KB
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
package com.wd.base.log;
import static com.wd.base.log.Utils.checkNotNull;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* CSV formatted file logging for Android.
* Writes to CSV the following data:
* epoch timestamp, ISO8601 timestamp (human-readable), log level, tag, log message.
*/
public class CsvFormatStrategy implements FormatStrategy {
private static final String NEW_LINE = System.getProperty("line.separator");
private static final String NEW_LINE_REPLACEMENT = " <br> ";
private static final String SEPARATOR = ",";
@NonNull
private final Date date;
@NonNull
private final SimpleDateFormat dateFormat;
@NonNull
private final LogStrategy logStrategy;
@Nullable
private final String tag;
private CsvFormatStrategy(@NonNull CsvFormatStrategy.Builder builder) {
checkNotNull(builder);
date = builder.date;
dateFormat = builder.dateFormat;
logStrategy = builder.logStrategy;
tag = builder.tag;
}
@NonNull
public static CsvFormatStrategy.Builder newBuilder() {
return new CsvFormatStrategy.Builder();
}
@Override
public void log(int priority, @Nullable String onceOnlyTag, @NonNull String message) {
checkNotNull(message);
String tag = formatTag(onceOnlyTag);
date.setTime(System.currentTimeMillis());
StringBuilder builder = new StringBuilder();
// machine-readable date/time
builder.append(Long.toString(date.getTime()));
// human-readable date/time
builder.append(SEPARATOR);
builder.append(dateFormat.format(date));
// level
builder.append(SEPARATOR);
builder.append(Utils.logLevel(priority));
// tag
builder.append(SEPARATOR);
builder.append(tag);
// message
if (message.contains(NEW_LINE)) {
// a new line would break the CSV format, so we replace it here
message = message.replaceAll(NEW_LINE, NEW_LINE_REPLACEMENT);
}
builder.append(SEPARATOR);
builder.append(message);
// new line
builder.append(NEW_LINE);
logStrategy.log(priority, tag, builder.toString());
}
@Nullable
private String formatTag(@Nullable String tag) {
if (!Utils.isEmpty(tag) && !Utils.equals(this.tag, tag)) {
return this.tag + "-" + tag;
}
return this.tag;
}
public static final class Builder {
private static final int MAX_BYTES = 500 * 1024; // 500K averages to a 4000 lines per file
Date date;
SimpleDateFormat dateFormat;
LogStrategy logStrategy;
String tag = "PRETTY_LOGGER";
private Builder() {
}
@NonNull
public CsvFormatStrategy.Builder date(@Nullable Date val) {
date = val;
return this;
}
@NonNull
public CsvFormatStrategy.Builder dateFormat(@Nullable SimpleDateFormat val) {
dateFormat = val;
return this;
}
@NonNull
public CsvFormatStrategy.Builder logStrategy(@Nullable LogStrategy val) {
logStrategy = val;
return this;
}
@NonNull
public CsvFormatStrategy.Builder tag(@Nullable String tag) {
this.tag = tag;
return this;
}
@NonNull
public CsvFormatStrategy build() {
if (date == null) {
date = new Date();
}
if (dateFormat == null) {
dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss.SSS", Locale.UK);
}
if (logStrategy == null) {
String diskPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String folder = diskPath + File.separatorChar + "logger";
HandlerThread ht = new HandlerThread("AndroidFileLogger." + folder);
ht.start();
Handler handler = new DiskLogStrategy.WriteHandler(ht.getLooper(), folder, MAX_BYTES);
logStrategy = new DiskLogStrategy(handler);
}
return new CsvFormatStrategy(this);
}
}
}