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

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

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

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

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

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

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

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

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

不罗嗦,上代码:

public class CommonLog {
	private String tag = "CommonLog";
	public static int logLevel = Log.VERBOSE;
	public static boolean isDebug = true;
	
	public CommonLog() { }	
	
	public CommonLog(String tag) {
		this.tag = tag;
	}
	
	public void setTag(String tag) {
		this.tag = tag;
	}
	
	private String getFunctionName() {
        StackTraceElement[] sts = Thread.currentThread().getStackTrace();
        
        if (sts == null) {
            return null;
        }
        
        
        for (StackTraceElement st:sts) {
            if (st.isNativeMethod()) {
                continue;
            }

            if (st.getClassName().equals(Thread.class.getName())) {
                continue;
            }

            if (st.getClassName().equals(this.getClass().getName())) {
                continue;
            }

            return "["+Thread.currentThread().getName()+"("+Thread.currentThread().getId()+"): "+st.getFileName()+":"+st.getLineNumber()+"]";
        }
        
        return null;
	}

	public void info(Object str) {
	    if (logLevel <= Log.INFO) {	        
	        String name = getFunctionName();
	        String ls=(name==null?str.toString():(name+" - "+str));
	        Log.i(tag, ls);
	    }
	}
	
	public void i(Object str) {
		if (isDebug) {
			info(str);
		}
	}
	
	public void verbose(Object str) {
        if (logLevel <= Log.VERBOSE) {
            String name = getFunctionName();
            String ls=(name==null?str.toString():(name+" - "+str));
            Log.v(tag, ls);    
        }
	}
	
	public void v(Object str) {
		if (isDebug) {
			verbose(str);
		}
    }
	
	public void warn(Object str) {
	    if (logLevel <= Log.WARN) {
            String name = getFunctionName();
            String ls=(name==null?str.toString():(name+" - "+str));
            Log.w(tag, ls);
	    }
	}
	
	public void w(Object str) {
		if (isDebug) {
			warn(str);
		}
    }
	
	public void error(Object str) {
        if (logLevel <= Log.ERROR) {            
            String name = getFunctionName();
            String ls=(name==null?str.toString():(name+" - "+str));
            Log.e(tag, ls);
        }
	}
	
	public void error(Exception ex) {
	    if (logLevel <= Log.ERROR) {
	        StringBuffer sb = new StringBuffer();
	        String name = getFunctionName();
	        StackTraceElement[] sts = ex.getStackTrace();

	        if (name != null) {
                sb.append(name+" - "+ex+"\r\n");
            } else {
                sb.append(ex+"\r\n");
            }
	        
	        if (sts != null && sts.length > 0) {
	            for (StackTraceElement st:sts) {
	                if (st != null) {
	                    sb.append("[ "+st.getFileName()+":"+st.getLineNumber()+" ]\r\n");
	                }
	            }
	        }
	        
	        Log.e(tag, sb.toString());
	    }
	}
	
    public void e(Object str) {
    	if (isDebug) {
    		error(str);
    	}
    }

    public void e(Exception ex) {
    	if (isDebug) {
    		error(ex);
    	}
    }
	
	public void debug(Object str) {
        if (logLevel <= Log.DEBUG) {
            String name = getFunctionName();
            String ls = (name == null?str.toString():(name+" - "+str));
            Log.d(tag, ls);
        }
	}
	
	public void d(Object str) {
		if (isDebug) {
			debug(str);
		}
    }
}

看Activity里的调用:

public class DebugDemoActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
 
	private CommonLog mCommonLog = LogFactory.createLog();
	private Button mBtn1;
	private Button mBtn2;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initView();
        
        mCommonLog.e("onCreate...");
        
    }


	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
		 mCommonLog.e("onStart...");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		 mCommonLog.e("onResume...");
	}



	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		 mCommonLog.e("onPause...");
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
		 mCommonLog.e("onStop...");
	}
	
    
    @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		 mCommonLog.e("onDestroy...");
	}

	public void  initView()
    {
    	mBtn1 = (Button) findViewById(R.id.button1);
    	mBtn1.setOnClickListener(this);
    	
    	mBtn2 = (Button) findViewById(R.id.button2);
    	mBtn2.setOnClickListener(this);
    }

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch(view.getId())
		{
		case R.id.button1:
		{
			 mCommonLog.e("R.id.button1		onClick...");
		}
			break;
		case R.id.button2:
		{
			SubThread subThread = new SubThread();
			subThread.start();
		}
			break;
		default:
			break;
		}
	}
}

最后看效果图:


其实上面代码可以经过修改,将方法名称也打印出来,这样效果可能会更好!

原文出自:http://blog.csdn.net/geniuseoe2012/article/details/7820410 其中有源码下载地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值