Android 自定義Log

import static android.util.Log.DEBUG;
import static android.util.Log.ERROR;
import static android.util.Log.INFO;
import static android.util.Log.WARN;

/**
 * Convenient wrapper for Android logs.
 *
 * @author Guillaume Beraudo
 */
public final class Log {

	public static final String TAG = "LogDemo";
	private static final boolean useIsLoggable = false;

	@SuppressWarnings(value="all")
	private static boolean isLoggable(int level) {
		return !useIsLoggable || android.util.Log.isLoggable(TAG, level);
	}

	public static void i(Object...objects) {
		if (isLoggable(INFO)) {
			android.util.Log.i(TAG, toString(objects));
		}
	}
	public static void i(Throwable t, Object...objects) {
		if (isLoggable(INFO)) {
			android.util.Log.i(TAG, toString(objects), t);
		}
	}

	
	public static void d(Object...objects) {
		if (isLoggable(DEBUG)) {
			android.util.Log.d(TAG, toString(objects));
		}
	}
	public static void d(Throwable t, Object...objects) {
		if (isLoggable(DEBUG)) {
			android.util.Log.d(TAG, toString(objects), t);
		}
	}
	
	public static void w(Object...objects) {
		if (isLoggable(WARN)) {
			android.util.Log.w(TAG, toString(objects));
		}
	}
	public static void w(Throwable t, Object...objects) {
		if (isLoggable(WARN)) {
			android.util.Log.w(TAG, toString(objects), t);
		}
	}
	
	public static void e(Object...objects) {
		if (isLoggable(ERROR)) {
			android.util.Log.e(TAG, toString(objects));
		}
	}
	public static void e(Throwable t, Object...objects) {
		if (isLoggable(ERROR)) {
			android.util.Log.e(TAG, toString(objects), t);
		}
	}

	/**
	 * @throws RuntimeException always throw after logging the error message.
	 */
	public static void f(Object...objects) {
		if (isLoggable(ERROR)) {
			android.util.Log.e(TAG, toString(objects));
			throw new RuntimeException("Fatal error : " + toString(objects));
		}
	}
	/**
	 * @throws RuntimeException always throw after logging the error message.
	 */
	public static void f(Throwable t, Object...objects) {
		if (isLoggable(ERROR)) {
			android.util.Log.e(TAG, toString(objects), t);
			throw new RuntimeException("Fatal error : " + toString(objects), t);
		}
	}

	private static String toString(Object...objects) {
		StringBuilder sb = new StringBuilder();
		for (Object o : objects) {
			sb.append(o);
		}
		return sb.toString();
	}
}


public final class Log {
	

	private static boolean sIsLogEnabled = true;// 是否打開LOG

	private static String sApplicationTag = "LogDemo";// LOG默認TAG

	private static final String TAG_CONTENT_PRINT = "%s:%s.%s:%d";

	private static StackTraceElement getCurrentStackTraceElement() {
		return Thread.currentThread().getStackTrace()[4];

	}

	//打印LOG
	public static void trace() {
		if (sIsLogEnabled) {
			android.util.Log.d(sApplicationTag,
					getContent(getCurrentStackTraceElement()));
		}
	}

	//獲取LOG
	private static String getContent(StackTraceElement trace) {
		return String.format(TAG_CONTENT_PRINT, sApplicationTag,
				trace.getClassName(), trace.getMethodName(),
				trace.getLineNumber());
	}
	//打印默認TAG的LOG
	public static void traceStack() {
		if (sIsLogEnabled) {
			traceStack(sApplicationTag, android.util.Log.ERROR);
		}
	}

	// 打印Log當前調用棧信息
	public static void traceStack(String tag, int priority) {

		if (sIsLogEnabled) {
			StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
			android.util.Log.println(priority, tag, stackTrace[4].toString());
			StringBuilder str = new StringBuilder();
			String prevClass = null;
			for (int i = 5; i < stackTrace.length; i++) {
				String className = stackTrace[i].getFileName();
				int idx = className.indexOf(".java");
				if (idx >= 0) {
					className = className.substring(0, idx);
				}
				if (prevClass == null || !prevClass.equals(className)) {

					str.append(className.substring(0, idx));

				}
				prevClass = className;
				str.append(".").append(stackTrace[i].getMethodName())
						.append(":").append(stackTrace[i].getLineNumber())
						.append("->");
			}
			android.util.Log.println(priority, tag, str.toString());
		}
	}
	//指定TAG和指定內容的方法
	public static void d(String tag, String msg) {
		if (sIsLogEnabled) {
			android.util.Log.d(tag, getContent(getCurrentStackTraceElement())+">"+msg);
		}
	}
	//默認TAG和制定內容的方法
	public static void d(String msg) {
		if (sIsLogEnabled) {
			android.util.Log.d(sApplicationTag, getContent(getCurrentStackTraceElement())+">"+msg);
		}
	}
	//不同級別的Log
	public static void i(String tag,String msg){
		if (sIsLogEnabled) {
			android.util.Log.d(sApplicationTag, getContent(getCurrentStackTraceElement())+">"+msg);
		}
	}
	public static void w(String tag,String msg){
		if (sIsLogEnabled) {
			android.util.Log.d(sApplicationTag, getContent(getCurrentStackTraceElement())+">"+msg);
		}
	}
	public static void e(String tag,String msg){
		if (sIsLogEnabled) {
			android.util.Log.d(sApplicationTag, getContent(getCurrentStackTraceElement())+">"+msg);
		}
	}
	

}





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