-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCommonLog.java
More file actions
210 lines (188 loc) · 6.66 KB
/
CommonLog.java
File metadata and controls
210 lines (188 loc) · 6.66 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package common.base.utils;
import android.util.Log;
import com.fee.thexselector.BuildConfig;
/**
* 日志输出
* <br/>
* 2015年12月23日-上午11:41:07
* @author lifei
*/
public final class CommonLog {
// public final static boolean ISDEBUG = BuildConfig.DEBUG;
public static boolean ISDEBUG = BuildConfig.DEBUG;
public static void logEnable(boolean toEnable) {
ISDEBUG = toEnable;
}
public static void w(String tag, String content) {
if (ISDEBUG) {
android.util.Log.w(tag, content);
}
}
public static void w(final String tag, Object... objs) {
if (ISDEBUG) {
android.util.Log.w(tag, getInfo(objs));
}
}
public static void i(String tag, String content) {
if (ISDEBUG) {
android.util.Log.i(tag, content);
}
}
public static void i(final String tag, Object... objs) {
if (ISDEBUG) {
android.util.Log.i(tag, getInfo(objs));
}
}
public static void d(String tag, String content) {
if (ISDEBUG) {
android.util.Log.d(tag, content);
}
}
public static void d(final String tag, Object... objs) {
if (ISDEBUG) {
android.util.Log.d(tag, getInfo(objs));
}
}
public static void e(String tag, String content) {
if (ISDEBUG) {
android.util.Log.e(tag, content);
}
}
public static void e(String tag, String content, Throwable e) {
if (ISDEBUG) {
android.util.Log.e(tag, content, e);
}
}
public static void e(final String tag, Object... objs) {
if (ISDEBUG) {
android.util.Log.e(tag, getInfo(objs));
}
}
public static void e(String logTag, String logContent, boolean appendStackInfo) {
if (!appendStackInfo) {
e(logTag,logContent);
}
else {
StringBuilder sb = new StringBuilder();
appendStack(sb);
sb.append(logContent);
e(logTag, sb.toString());
}
}
public static void i(String logTag, String logContent, boolean appendStackInfo) {
if (!appendStackInfo) {
i(logTag,logContent);
}
else {
StringBuilder sb = new StringBuilder();
appendStack(sb);
sb.append(logContent);
i(logTag, sb.toString());
}
}
public static String getInfo(Object... objs) {
if (objs == null) {
return "";
}
StringBuilder sb = new StringBuilder();
for (Object object : objs) {
sb.append(object);
}
return sb.toString();
}
public static void sysOut(Object msg) {
if (ISDEBUG) {
System.out.println(msg);
}
}
public static void sysErr(Object msg) {
if (ISDEBUG) {
System.err.println(msg);
}
}
private static final int START_STACK_INDEX = 4;
private static final int PRINT_STACK_COUNT = 2;
private static void appendStack(StringBuilder sb) {
StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
if (stacks != null && stacks.length > START_STACK_INDEX) {
int lastIndex = Math.min(stacks.length - 1, START_STACK_INDEX + PRINT_STACK_COUNT);
for (int i=lastIndex; i >= START_STACK_INDEX; i--) {
if (stacks[i] == null) {
continue;
}
String fileName = stacks[i].getFileName();
if (fileName != null) {
int dotIndx = fileName.indexOf('.');
if (dotIndx > 0) {
fileName = fileName.substring(0, dotIndx);
}
}
sb.append(fileName);
sb.append('(');
sb.append(stacks[i].getLineNumber());
sb.append(")");
sb.append("->");
}
StackTraceElement stackTraceElement = stacks[START_STACK_INDEX];
if (stackTraceElement != null) {
sb.append(stackTraceElement.getMethodName());
}
}
sb.append('\n');
}
/**
* 由于Android系统对日志长度有限制的,最大长度为4K(注意是字符串的长度),超过这个范围的自动截断,会出现打印不全的情况
* @param logLevelFlagChar Log的输出级别字符标志;取值: 'd'; 'i'; 'e'; 'w'; 'v'
* @param logTag Log的tag
* @param logContent Log的内容
*/
public static void fullLog(char logLevelFlagChar, String logTag, String logContent) {
if (ISDEBUG && !CheckUtil.isEmpty(logContent)) {
int segmentSize = 3 * 1024;
int len = logContent.length();
if (len <= segmentSize) {
log(logLevelFlagChar,logTag,logContent);
}
else {
while (logContent.length() > segmentSize) {
String suitableLogContent = logContent.substring(0, segmentSize);
log(logLevelFlagChar, logTag, suitableLogContent);
logContent = logContent.substring(segmentSize);
// StringBuilder sb = new StringBuilder(logContent);
// sb.delete(0, segmentSize);
// logContent = sb.toString();
// sb.append(logContent, segmentSize, logContent.length());
// logContent = logContent.replace(logContent, "");//这个效率低
}
log(logLevelFlagChar, logTag, logContent);
}
}
}
public static void iFullLog(String logTag, String logContent) {
fullLog('i', logTag, logContent);
}
public static void iFullLog(String logTag, Object... logObj) {
iFullLog(logTag, getInfo(logObj));
}
public static void log(char logLevelFlagChar, String logTag, String logContent) {
if (ISDEBUG) {
switch (logLevelFlagChar) {
case 'i':
i(logTag, logContent);
break;
case 'e':
e(logTag,logContent);
break;
case 'd':
d(logTag,logContent);
break;
case 'w':
w(logTag,logContent);
break;
case 'v':
Log.v(logTag, logContent);
break;
}
}
}
}