实用的Log打印类封装,快速定位源码(android)

相信众多android开发者在开发程序的过程中会经常用到Log打印信息

以方便了解当前程序的运行状况以及在出现BUG的时候能够快速定位问题

大多数童鞋会使用官方的打印log的方法,设置TAG,然后在Eclipse里面设置过滤标签,切换来回的看Log,但这样却效率很低;

下面分享一个Log打印信息的封装类,主要提供以下功能:

1.使用一个标签来标记当前的AP(避免设置过多的TAG来过滤显示不同Java文件下的Log)

2.显示当前的线程ID,用于辨别主线程还是子线程

3.显示当前的Java文件与打印log的行号,便于快速定位到源文件

4.最后显示你设置打印出来的信息

不罗嗦,上代码:

[java]  view plain copy
  1. public class CommonLog {  
  2.     private String tag = "CommonLog";  
  3.     public static int logLevel = Log.VERBOSE;  
  4.     public static boolean isDebug = true;  
  5.       
  6.     public CommonLog() { }    
  7.       
  8.     public CommonLog(String tag) {  
  9.         this.tag = tag;  
  10.     }  
  11.       
  12.     public void setTag(String tag) {  
  13.         this.tag = tag;  
  14.     }  
  15.       
  16.     private String getFunctionName() {  
  17.         StackTraceElement[] sts = Thread.currentThread().getStackTrace();  
  18.           
  19.         if (sts == null) {  
  20.             return null;  
  21.         }  
  22.           
  23.           
  24.         for (StackTraceElement st:sts) {  
  25.             if (st.isNativeMethod()) {  
  26.                 continue;  
  27.             }  
  28.   
  29.             if (st.getClassName().equals(Thread.class.getName())) {  
  30.                 continue;  
  31.             }  
  32.   
  33.             if (st.getClassName().equals(this.getClass().getName())) {  
  34.                 continue;  
  35.             }  
  36.   
  37.             return "["+Thread.currentThread().getName()+"("+Thread.currentThread().getId()+"): "+st.getFileName()+":"+st.getLineNumber()+"]";  
  38.         }  
  39.           
  40.         return null;  
  41.     }  
  42.   
  43.     public void info(Object str) {  
  44.         if (logLevel <= Log.INFO) {            
  45.             String name = getFunctionName();  
  46.             String ls=(name==null?str.toString():(name+" - "+str));  
  47.             Log.i(tag, ls);  
  48.         }  
  49.     }  
  50.       
  51.     public void i(Object str) {  
  52.         if (isDebug) {  
  53.             info(str);  
  54.         }  
  55.     }  
  56.       
  57.     public void verbose(Object str) {  
  58.         if (logLevel <= Log.VERBOSE) {  
  59.             String name = getFunctionName();  
  60.             String ls=(name==null?str.toString():(name+" - "+str));  
  61.             Log.v(tag, ls);      
  62.         }  
  63.     }  
  64.       
  65.     public void v(Object str) {  
  66.         if (isDebug) {  
  67.             verbose(str);  
  68.         }  
  69.     }  
  70.       
  71.     public void warn(Object str) {  
  72.         if (logLevel <= Log.WARN) {  
  73.             String name = getFunctionName();  
  74.             String ls=(name==null?str.toString():(name+" - "+str));  
  75.             Log.w(tag, ls);  
  76.         }  
  77.     }  
  78.       
  79.     public void w(Object str) {  
  80.         if (isDebug) {  
  81.             warn(str);  
  82.         }  
  83.     }  
  84.       
  85.     public void error(Object str) {  
  86.         if (logLevel <= Log.ERROR) {              
  87.             String name = getFunctionName();  
  88.             String ls=(name==null?str.toString():(name+" - "+str));  
  89.             Log.e(tag, ls);  
  90.         }  
  91.     }  
  92.       
  93.     public void error(Exception ex) {  
  94.         if (logLevel <= Log.ERROR) {  
  95.             StringBuffer sb = new StringBuffer();  
  96.             String name = getFunctionName();  
  97.             StackTraceElement[] sts = ex.getStackTrace();  
  98.   
  99.             if (name != null) {  
  100.                 sb.append(name+" - "+ex+"\r\n");  
  101.             } else {  
  102.                 sb.append(ex+"\r\n");  
  103.             }  
  104.               
  105.             if (sts != null && sts.length > 0) {  
  106.                 for (StackTraceElement st:sts) {  
  107.                     if (st != null) {  
  108.                         sb.append("[ "+st.getFileName()+":"+st.getLineNumber()+" ]\r\n");  
  109.                     }  
  110.                 }  
  111.             }  
  112.               
  113.             Log.e(tag, sb.toString());  
  114.         }  
  115.     }  
  116.       
  117.     public void e(Object str) {  
  118.         if (isDebug) {  
  119.             error(str);  
  120.         }  
  121.     }  
  122.   
  123.     public void e(Exception ex) {  
  124.         if (isDebug) {  
  125.             error(ex);  
  126.         }  
  127.     }  
  128.       
  129.     public void debug(Object str) {  
  130.         if (logLevel <= Log.DEBUG) {  
  131.             String name = getFunctionName();  
  132.             String ls = (name == null?str.toString():(name+" - "+str));  
  133.             Log.d(tag, ls);  
  134.         }  
  135.     }  
  136.       
  137.     public void d(Object str) {  
  138.         if (isDebug) {  
  139.             debug(str);  
  140.         }  
  141.     }  
  142. }  


 

看ACTIVITY里的调用:

[java]  view plain copy
  1. public class DebugDemoActivity extends Activity implements OnClickListener{  
  2.     /** Called when the activity is first created. */  
  3.    
  4.     private CommonLog mCommonLog = LogFactory.createLog();  
  5.     private Button mBtn1;  
  6.     private Button mBtn2;  
  7.       
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11.           
  12.         initView();  
  13.           
  14.         mCommonLog.e("onCreate...");  
  15.           
  16.     }  
  17.   
  18.   
  19.     @Override  
  20.     protected void onStart() {  
  21.         // TODO Auto-generated method stub  
  22.         super.onStart();  
  23.           
  24.          mCommonLog.e("onStart...");  
  25.     }  
  26.   
  27.     @Override  
  28.     protected void onResume() {  
  29.         // TODO Auto-generated method stub  
  30.         super.onResume();  
  31.           
  32.          mCommonLog.e("onResume...");  
  33.     }  
  34.   
  35.   
  36.   
  37.     @Override  
  38.     protected void onPause() {  
  39.         // TODO Auto-generated method stub  
  40.         super.onPause();  
  41.           
  42.          mCommonLog.e("onPause...");  
  43.     }  
  44.       
  45.     @Override  
  46.     protected void onStop() {  
  47.         // TODO Auto-generated method stub  
  48.         super.onStop();  
  49.           
  50.          mCommonLog.e("onStop...");  
  51.     }  
  52.       
  53.       
  54.     @Override  
  55.     protected void onDestroy() {  
  56.         // TODO Auto-generated method stub  
  57.         super.onDestroy();  
  58.           
  59.          mCommonLog.e("onDestroy...");  
  60.     }  
  61.   
  62.     public void  initView()  
  63.     {  
  64.         mBtn1 = (Button) findViewById(R.id.button1);  
  65.         mBtn1.setOnClickListener(this);  
  66.           
  67.         mBtn2 = (Button) findViewById(R.id.button2);  
  68.         mBtn2.setOnClickListener(this);  
  69.     }  
  70.   
  71.     @Override  
  72.     public void onClick(View view) {  
  73.         // TODO Auto-generated method stub  
  74.         switch(view.getId())  
  75.         {  
  76.         case R.id.button1:  
  77.         {  
  78.              mCommonLog.e("R.id.button1     onClick...");  
  79.         }  
  80.             break;  
  81.         case R.id.button2:  
  82.         {  
  83.             SubThread subThread = new SubThread();  
  84.             subThread.start();  
  85.         }  
  86.             break;  
  87.         default:  
  88.             break;  
  89.         }  
  90.     }  
  91. }  

最后看效果图:

 

下面附上工程链接:

http://download.csdn.net/detail/geniuseoe2012/4470104

欲了解更多android logcat的使用,请看这篇博文:

http://blog.csdn.net/geniuseoe2012/article/details/7820366

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值