android 简单实用的Log打印类封装,助你快速定位问题

基于Android实现一段音频和视频播放的安卓代码
http://www.eoeandroid.com/thread-195269-1-1.html
Android手机测周围环境分贝的功能源码
http://www.eoeandroid.com/thread-194968-1-1.html
代码仓库——游戏源码汇总
http://www.eoeandroid.com/thread-195034-1-1.html
滑动切换加载网络图片可放大缩小例子
http://www.eoeandroid.com/thread-194644-1-1.html
实现uc和墨迹天气那样的左右拖动效果
http://www.eoeandroid.com/thread-194450-1-1.html
 
 
相信众多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().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;

}

}

}

最后看效果图:

 

android 简单实用的Log打印类封装,助你快速定位问题

源码下载:DebugDemo

 

你可能感兴趣的:(android)