Android输出log的包装

本文中的log包装来源于github一个项目中,具体位置我给忘了.经过此包装后可以方便查看到log所在的类名,方法名,行数等信息.‘

 1 public class DLog {
 2     static String className;
 3     static String methodName;
 4     static int lineNumber;
 5      
 6     private DLog(){
 7         /* Protect from instantiations */
 8     }
 9  
10     public static boolean isDebuggable() {
11         return BuildConfig.DEBUG;
12     }
13  
14     private static String createLog( String log ) {
15          
16         StringBuffer buffer = new StringBuffer();
17         buffer.append("[");
18         buffer.append(methodName);
19         buffer.append(":");
20         buffer.append(lineNumber);
21         buffer.append("]");
22         buffer.append(log);
23  
24         return buffer.toString();
25     }
26      
27     private static void getMethodNames(StackTraceElement[] sElements){
28         className = sElements[1].getFileName();
29         methodName = sElements[1].getMethodName();
30         lineNumber = sElements[1].getLineNumber();
31     }
32  
33     public static void e(String message){
34         if (!isDebuggable()){
35             return;
36         }
37  
38         getMethodNames(new Throwable().getStackTrace());
39         Log.e(className, createLog(message));
40     }
41  
42     public static void i(String message){
43         if (!isDebuggable()){
44             return;
45         }
46  
47         getMethodNames(new Throwable().getStackTrace());
48         Log.i(className, createLog(message));
49     }
50      
51     public static void d(String message){
52         if (!isDebuggable()){
53             return;
54         }
55  
56         getMethodNames(new Throwable().getStackTrace());
57         Log.d(className, createLog(message));
58     }
59      
60     public static void v(String message){
61         if (!isDebuggable()){
62             return;
63         }
64          
65         getMethodNames(new Throwable().getStackTrace());
66         Log.v(className, createLog(message));
67     }
68      
69     public static void w(String message){
70         if (!isDebuggable()){
71             return;
72         }
73  
74         getMethodNames(new Throwable().getStackTrace());
75         Log.w(className, createLog(message));
76     }
77      
78     public static void wtf(String message){
79         if (!isDebuggable()){
80             return;
81         }
82  
83         getMethodNames(new Throwable().getStackTrace());
84         Log.wtf(className, createLog(message));
85     }
86 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。