频道栏目
IT货架 > > 正文
Android输出log的包装
网友分享于:Jan 1, 1970 8:00:00 AM    来源: IT货架   

标签:

本文中的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 }

 

Android输出log的包装

标签:

相关板块推荐 iPhone WindowsPhone

广告服务联系QQ:1134687142 | 网站地图

版权所有: IT货架- 内容来自互联网,仅供用于技术学习,请遵循相关法律法规. 京ICP备11030978号-1