Skip to content

Commit 5027f98

Browse files
committed
新增Log拦截器模块,完善Logg全局监听回调管理,完善注释说明,优化了部分类结构
1 parent f4bf7ff commit 5027f98

File tree

8 files changed

+145
-83
lines changed

8 files changed

+145
-83
lines changed

Log/src/main/java/com/logg/Logg.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.logg.config.LoggConfiguration;
44
import com.logg.interceptor.Interceptor;
5+
import com.logg.interceptor.LoggInterceptor;
56
import com.logg.printer.manager.IPrinterManager;
67
import com.logg.printer.manager.PrinterManager;
78

@@ -143,11 +144,11 @@ public static void xml(String tag, Object object) {
143144
printer.xml(tag, object);
144145
}
145146

146-
public static void addInterceptor(Interceptor interceptor) {
147+
public static void addInterceptor(LoggInterceptor interceptor) {
147148
printer.addInterceptor(interceptor);
148149
}
149150

150-
public static void removeInterceptor(Interceptor interceptor) {
151+
public static void removeInterceptor(LoggInterceptor interceptor) {
151152
printer.removeInterceptor(interceptor);
152153
}
153154

Log/src/main/java/com/logg/config/LoggConfiguration.java

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.logg.config;
22

3+
import com.logg.interceptor.LoggInterceptor;
34
import com.logg.parser.Parser;
45

56
import java.util.ArrayList;
@@ -15,12 +16,47 @@ public class LoggConfiguration {
1516

1617
private List<Parser> parsers = new ArrayList<>();
1718

19+
private List<LoggInterceptor> interceptors;
20+
1821
public LoggConfiguration(Buidler buidler) {
1922
this.debug = buidler.debug;
2023
this.tag = buidler.tag;
2124
this.parsers = buidler.parsers;
2225

2326
this.addParserClass(LoggConstant.DEFAULT_PARSER_CLASS);
27+
28+
this.interceptors = buidler.interceptors;
29+
}
30+
31+
32+
public List<Parser> getParsers() {
33+
return parsers;
34+
}
35+
36+
/**
37+
* Add a custom parser
38+
*/
39+
public LoggConfiguration addParserClass(Class<? extends Parser>... classes) {
40+
for (Class<? extends Parser> cla : classes) {
41+
try {
42+
parsers.add(0, cla.newInstance());
43+
} catch (Exception e) {
44+
e.printStackTrace();
45+
}
46+
}
47+
return this;
48+
}
49+
50+
public boolean isDebug() {
51+
return debug;
52+
}
53+
54+
public String getTag() {
55+
return tag;
56+
}
57+
58+
public List<LoggInterceptor> getInterceptors() {
59+
return interceptors;
2460
}
2561

2662
public static class Buidler {
@@ -42,6 +78,11 @@ public static class Buidler {
4278
*/
4379
private List<Parser> parsers = new ArrayList<>();
4480

81+
/**
82+
* LoggInterceptors
83+
*/
84+
private List<LoggInterceptor> interceptors = new ArrayList<>();
85+
4586
public Buidler() {
4687
;
4788
}
@@ -59,34 +100,13 @@ public Buidler setDebug(boolean debug) {
59100
return this;
60101
}
61102

62-
public LoggConfiguration build() {
63-
return new LoggConfiguration(this);
103+
public Buidler addInterceptor(LoggInterceptor interceptor) {
104+
this.interceptors.add(interceptor);
105+
return this;
64106
}
65-
}
66-
67-
public List<Parser> getParsers() {
68-
return parsers;
69-
}
70107

71-
/**
72-
* Add a custom parser
73-
*/
74-
public LoggConfiguration addParserClass(Class<? extends Parser>... classes) {
75-
for (Class<? extends Parser> cla : classes) {
76-
try {
77-
parsers.add(0, cla.newInstance());
78-
} catch (Exception e) {
79-
e.printStackTrace();
80-
}
108+
public LoggConfiguration build() {
109+
return new LoggConfiguration(this);
81110
}
82-
return this;
83-
}
84-
85-
public boolean isDebug() {
86-
return debug;
87-
}
88-
89-
public String getTag() {
90-
return tag;
91111
}
92112
}

Log/src/main/java/com/logg/interceptor/LoggStructure.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class LoggStructure {
1212
// 前缀
1313
private String tag;
1414
// 内容
15-
private Object object;
15+
private String object;
1616

1717
// 线程信息,如果线程信息被禁用则为Null
1818
private String thread;
@@ -21,11 +21,11 @@ private LoggStructure() {
2121

2222
}
2323

24-
public LoggStructure(Type type, String tag, Object object) {
24+
public LoggStructure(Type type, String tag, String object) {
2525
this(type, tag, object, null);
2626
}
2727

28-
public LoggStructure(Type type, String tag, Object object, String thread) {
28+
public LoggStructure(Type type, String tag, String object, String thread) {
2929
this.type = type;
3030
this.tag = tag;
3131
this.object = object;
@@ -48,11 +48,11 @@ public void setTag(String tag) {
4848
this.tag = tag;
4949
}
5050

51-
public Object getObject() {
51+
public String getObject() {
5252
return object;
5353
}
5454

55-
public void setObject(Object object) {
55+
public void setObject(String object) {
5656
this.object = object;
5757
}
5858

Log/src/main/java/com/logg/printer/DefaultPrinter.java

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,98 @@
33
import android.util.Log;
44

55
import com.logg.config.LoggConfiguration;
6+
import com.logg.interceptor.Interceptor;
7+
import com.logg.interceptor.LoggInterceptor;
8+
import com.logg.interceptor.LoggStructure;
69
import com.logg.interceptor.callback.GlobalCallback;
710

11+
import java.util.ArrayList;
12+
import java.util.List;
13+
814
/**
915
* Default Printer
1016
*/
1117
public class DefaultPrinter implements Printer {
1218

1319
private LoggConfiguration configuration;
1420

21+
/**
22+
* LoggInterceptors
23+
*/
24+
private List<LoggInterceptor> interceptors;
25+
1526
public DefaultPrinter(LoggConfiguration configuration) {
1627
this.configuration = configuration;
28+
interceptors = configuration.getInterceptors();
1729
}
1830

1931
@Override
2032
public void printer(Type type, String tag, String object) {
21-
switch (type) {
33+
LoggStructure item = new LoggStructure(type, tag, object, null);
34+
if (interceptors != null) {
35+
for (Interceptor interceptor : interceptors) {
36+
if (interceptor.isLoggable(type)) {
37+
item = interceptor.intercept(item);
38+
if (item == null) {
39+
throw new NullPointerException("LoggCallback == null");
40+
}
41+
42+
if (item.getTag() == null) {
43+
throw new NullPointerException("Tag == null, You can modify Tag, but can not empty Tag.");
44+
}
45+
46+
if (item.getObject() == null) {
47+
throw new NullPointerException("Object == null, You can modify the log information, but you can not clear the log information.");
48+
}
49+
}
50+
}
51+
}
52+
53+
switch (item.getType()) {
2254
case V:
23-
Log.v(tag, object);
55+
Log.v(item.getTag(), item.getObject());
2456
break;
2557
case D:
26-
Log.d(tag, object);
58+
Log.d(item.getTag(), item.getObject());
2759
break;
2860
case I:
29-
Log.i(tag, object);
61+
Log.i(item.getTag(), item.getObject());
3062
break;
3163
case W:
32-
Log.w(tag, object);
64+
Log.w(item.getTag(), item.getObject());
3365
break;
3466
case E:
35-
Log.e(tag, object);
67+
Log.e(item.getTag(), item.getObject());
3668
break;
3769
case WTF:
38-
Log.wtf(tag, object);
70+
Log.wtf(item.getTag(), item.getObject());
3971
break;
4072
case J:
41-
Log.d(tag, object);
73+
Log.d(item.getTag(), item.getObject());
4274
break;
4375
case X:
44-
Log.d(tag, object);
76+
Log.d(item.getTag(), item.getObject());
4577
break;
4678
default:
4779
break;
4880
}
4981

50-
GlobalCallback.getInstance().printerAll(type, tag, object);
82+
GlobalCallback.getInstance().printerAll(item.getType(), item.getTag(), item.getObject());
83+
}
84+
85+
public void addInterceptor(LoggInterceptor interceptor) {
86+
if (interceptor != null) {
87+
interceptors.add(interceptor);
88+
}
89+
}
90+
91+
public void removeInterceptor(LoggInterceptor interceptor) {
92+
if (interceptor != null) {
93+
interceptors.remove(interceptor);
94+
}
95+
}
96+
97+
public void clearInterceptors() {
98+
interceptors.clear();
5199
}
52100
}

Log/src/main/java/com/logg/printer/manager/IPrinterManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.logg.printer.manager;
22

33
import com.logg.config.LoggConfiguration;
4-
import com.logg.interceptor.Interceptor;
4+
import com.logg.interceptor.LoggInterceptor;
55

66
public interface IPrinterManager {
77

@@ -41,9 +41,9 @@ public interface IPrinterManager {
4141

4242
void xml(String tag, Object object);
4343

44-
void addInterceptor(Interceptor interceptor);
44+
void addInterceptor(LoggInterceptor interceptor);
4545

46-
void removeInterceptor(Interceptor interceptor);
46+
void removeInterceptor(LoggInterceptor interceptor);
4747

4848
void clearInterceptors();
4949

Log/src/main/java/com/logg/printer/manager/PrinterManager.java

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
import com.logg.config.LoggConfiguration;
77
import com.logg.config.LoggConstant;
88
import com.logg.interceptor.Interceptor;
9-
import com.logg.interceptor.LoggStructure;
9+
import com.logg.interceptor.LoggInterceptor;
1010
import com.logg.printer.DefaultPrinter;
1111
import com.logg.printer.JsonPrinter;
1212
import com.logg.printer.Printer;
1313
import com.logg.printer.Type;
1414
import com.logg.printer.XmlPrinter;
1515
import com.logg.util.Utils;
1616

17-
import java.util.ArrayList;
18-
import java.util.List;
1917
import java.util.logging.Logger;
2018

2119
/**
@@ -24,17 +22,12 @@
2422
public class PrinterManager implements IPrinterManager {
2523

2624
// Default Printer
27-
private Printer defaultPrinter = null;
25+
private DefaultPrinter defaultPrinter = null;
2826
// Json Printer
2927
private Printer jsonPrinter = null;
3028
// XML Printer
3129
private Printer xmlPrinter = null;
3230

33-
/**
34-
* LoggInterceptors
35-
*/
36-
private final List<Interceptor> interceptors = new ArrayList<>();
37-
3831
/**
3932
* Parameter configuration
4033
*/
@@ -165,22 +158,18 @@ public void xml(String tag, Object object) {
165158
}
166159

167160
@Override
168-
public void addInterceptor(Interceptor interceptor) {
169-
if (interceptor != null) {
170-
interceptors.add(interceptor);
171-
}
161+
public void addInterceptor(LoggInterceptor interceptor) {
162+
defaultPrinter.addInterceptor(interceptor);
172163
}
173164

174165
@Override
175-
public void removeInterceptor(Interceptor interceptor) {
176-
if (interceptor != null) {
177-
interceptors.remove(interceptor);
178-
}
166+
public void removeInterceptor(LoggInterceptor interceptor) {
167+
defaultPrinter.removeInterceptor(interceptor);
179168
}
180169

181170
@Override
182171
public void clearInterceptors() {
183-
interceptors.clear();
172+
defaultPrinter.clearInterceptors();
184173
}
185174

186175
/**
@@ -194,24 +183,6 @@ private synchronized void printer(Type type, String tag, Object object) {
194183
return;
195184
}
196185

197-
LoggStructure item = new LoggStructure(type, tag, object, null);
198-
for (Interceptor interceptor : interceptors) {
199-
if (interceptor.isLoggable(type)) {
200-
item = interceptor.intercept(item);
201-
if (item == null) {
202-
throw new NullPointerException("LoggCallback == null");
203-
}
204-
205-
if (item.getTag() == null) {
206-
throw new NullPointerException("Tag == null, You can modify Tag, but can not empty Tag.");
207-
}
208-
209-
if (item.getObject() == null) {
210-
throw new NullPointerException("Object == null, You can modify the log information, but you can not clear the log information.");
211-
}
212-
}
213-
}
214-
215186
switch (type) {
216187
case V:
217188
case D:

0 commit comments

Comments
 (0)