1
- package com .dovar .router_api ;
1
+ package com .dovar .router_api . utils ;
2
2
3
- import android .support .annotation .Nullable ;
4
3
import android .text .TextUtils ;
4
+ import android .util .Log ;
5
5
6
6
7
7
public class Debugger {
8
- public static final String LOG_TAG = "DRouter" ;
9
-
10
- public interface Logger {
11
-
12
- void d (String msg , Object ... args );
13
-
14
- void i (String msg , Object ... args );
15
-
16
- void w (String msg , Object ... args );
17
-
18
- void w (Throwable t );
19
-
20
- void e (String msg , Object ... args );
21
-
22
- void e (Throwable t );
23
- }
24
-
25
- @ Nullable
26
- private static Logger sLogger = new DefaultLogger ();
27
-
8
+ private static final String LOG_TAG = "DRouter" ;
28
9
private static boolean sEnableLog = false ;
29
10
30
- /**
31
- * 设置Logger
32
- */
33
- public static void setLogger (Logger logger ) {
34
- sLogger = logger ;
35
- }
36
-
37
11
/**
38
12
* Log开关。建议测试环境开启,线上环境应该关闭。
39
13
*/
@@ -44,46 +18,47 @@ public static void setEnableLog(boolean enableLog) {
44
18
public static void d (String msg , Object ... args ) {
45
19
if (!sEnableLog ) return ;
46
20
if (TextUtils .isEmpty (msg )) return ;
47
- if (sLogger != null ) {
48
- sLogger .d (msg , args );
49
- }
21
+ Log .d (Debugger .LOG_TAG , format (msg , args ));
50
22
}
51
23
52
24
public static void i (String msg , Object ... args ) {
53
25
if (!sEnableLog ) return ;
54
26
if (TextUtils .isEmpty (msg )) return ;
55
- if (sLogger != null ) {
56
- sLogger .i (msg , args );
57
- }
27
+ Log .i (Debugger .LOG_TAG , format (msg , args ));
58
28
}
59
29
60
30
public static void w (String msg , Object ... args ) {
61
31
if (!sEnableLog ) return ;
62
32
if (TextUtils .isEmpty (msg )) return ;
63
- if (sLogger != null ) {
64
- sLogger .w (msg , args );
65
- }
33
+ Log .w (Debugger .LOG_TAG , format (msg , args ));
66
34
}
67
35
68
36
public static void w (Throwable t ) {
69
37
if (!sEnableLog ) return ;
70
- if (sLogger != null ) {
71
- sLogger .w (t );
72
- }
38
+ if (t == null ) return ;
39
+ Log .w (Debugger .LOG_TAG , t );
73
40
}
74
41
75
42
public static void e (String msg , Object ... args ) {
76
43
if (!sEnableLog ) return ;
77
44
if (TextUtils .isEmpty (msg )) return ;
78
- if (sLogger != null ) {
79
- sLogger .e (msg , args );
80
- }
45
+ Log .e (Debugger .LOG_TAG , format (msg , args ));
81
46
}
82
47
83
48
public static void e (Throwable t ) {
84
49
if (!sEnableLog ) return ;
85
- if (sLogger != null ) {
86
- sLogger .e (t );
50
+ if (t == null ) return ;
51
+ Log .e (Debugger .LOG_TAG , "" , t );
52
+ }
53
+
54
+ private static String format (String msg , Object ... args ) {
55
+ if (args != null && args .length > 0 ) {
56
+ try {
57
+ return String .format (msg , args );
58
+ } catch (Throwable t ) {
59
+ e (t );
60
+ }
87
61
}
62
+ return msg ;
88
63
}
89
64
}
0 commit comments