【No10.】一個超便捷的Log工具類

沒廢話,上代碼:

 

package com.example.logutils;

import android.text.TextUtils;
import android.util.Log;

/**
 * Log工具,類似android.util.Log。 tag自動產生,格式:
 * customTagPrefix:className.methodName(L:lineNumber),
 * customTagPrefix爲空時只輸出:className.methodName(L:lineNumber) Author: Simon
 */
public class LogUtils {

	public static String customTagPrefix = "";// 自定義標記前綴

	private LogUtils() {
	}

	public static boolean debug = true;// log總開關

	public static boolean allowD = true;
	public static boolean allowE = true;
	public static boolean allowI = true;
	public static boolean allowV = true;
	public static boolean allowW = true;
	public static boolean allowWtf = true;

	private static String generateTag(StackTraceElement caller) {
		String tag = "%s.%s(L:%d)";
		String callerClazzName = caller.getClassName();
		callerClazzName = callerClazzName.substring(callerClazzName
				.lastIndexOf(".") + 1);
		tag = String.format(tag, callerClazzName, caller.getMethodName(),
				caller.getLineNumber());
		tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":"
				+ tag;
		return tag;
	}

	public static CustomLogger customLogger;

	public interface CustomLogger {
		void d(String tag, String content);

		void d(String tag, String content, Throwable tr);

		void e(String tag, String content);

		void e(String tag, String content, Throwable tr);

		void i(String tag, String content);

		void i(String tag, String content, Throwable tr);

		void v(String tag, String content);

		void v(String tag, String content, Throwable tr);

		void w(String tag, String content);

		void w(String tag, String content, Throwable tr);

		void w(String tag, Throwable tr);

		void wtf(String tag, String content);

		void wtf(String tag, String content, Throwable tr);

		void wtf(String tag, Throwable tr);
	}

	public static void d(String content) {
		if (debug) {
			if (!allowD)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.d(tag, content);
			} else {
				Log.d(tag, content);
			}
		}
	}

	public static void d(String content, Throwable tr) {
		if (debug) {
			if (!allowD)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.d(tag, content, tr);
			} else {
				Log.d(tag, content, tr);
			}
		}
	}

	public static void e(String content) {
		if (debug) {
			if (!allowE)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.e(tag, content);
			} else {
				Log.e(tag, content);
			}
		}
	}

	public static void e(String content, Throwable tr) {
		if (debug) {
			if (!allowE)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.e(tag, content, tr);
			} else {
				Log.e(tag, content, tr);
			}
		}
	}

	public static void i(String content) {
		if (debug) {
			if (!allowI)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.i(tag, content);
			} else {
				Log.i(tag, content);
			}
		}
	}

	public static void i(String content, Throwable tr) {
		if (debug) {
			if (!allowI)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.i(tag, content, tr);
			} else {
				Log.i(tag, content, tr);
			}
		}
	}

	public static void v(String content) {
		if (debug) {
			if (!allowV)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.v(tag, content);
			} else {
				Log.v(tag, content);
			}
		}
	}

	public static void v(String content, Throwable tr) {
		if (debug) {
			if (!allowV)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.v(tag, content, tr);
			} else {
				Log.v(tag, content, tr);
			}
		}
	}

	public static void w(String content) {
		if (debug) {
			if (!allowW)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.w(tag, content);
			} else {
				Log.w(tag, content);
			}
		}
	}

	public static void w(String content, Throwable tr) {
		if (debug) {
			if (!allowW)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.w(tag, content, tr);
			} else {
				Log.w(tag, content, tr);
			}
		}
	}

	public static void w(Throwable tr) {
		if (debug) {
			if (!allowW)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.w(tag, tr);
			} else {
				Log.w(tag, tr);
			}
		}
	}

	public static void wtf(String content) {
		if (debug) {
			if (!allowWtf)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.wtf(tag, content);
			} else {
				Log.wtf(tag, content);
			}
		}
	}

	public static void wtf(String content, Throwable tr) {
		if (debug) {
			if (!allowWtf)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.wtf(tag, content, tr);
			} else {
				Log.wtf(tag, content, tr);
			}
		}
	}

	public static void wtf(Throwable tr) {
		if (debug) {
			if (!allowWtf)
				return;
			StackTraceElement caller = getCallerStackTraceElement();
			String tag = generateTag(caller);

			if (customLogger != null) {
				customLogger.wtf(tag, tr);
			} else {
				Log.wtf(tag, tr);
			}
		}
	}

	public static StackTraceElement getCurrentStackTraceElement() {
		return Thread.currentThread().getStackTrace()[3];
	}

	public static StackTraceElement getCallerStackTraceElement() {
		return Thread.currentThread().getStackTrace()[4];
	}

}


下面貼用法:

 

package com.example.logutils;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//LogUtils.customTagPrefix = "Simon";//設定TAG  可選
		
		LogUtils.d("1");

	}

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

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

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		LogUtils.v("4");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		LogUtils.w("5");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		LogUtils.wtf("6");
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		LogUtils.w("7");
	}

}

 

效果圖:

 

這個是不設置TAG的

 

這個是設置TAG的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章