讓android日誌更有用些

原文地址:http://marspring.mobi/android-log/

android的log比起log4j等Java EE下的日誌有些差距,比如不能直接簡單設置就按等級打印,也不能再打印log裏顯示打印調用該log的類的信息,方法名,行號等。這裏利用StackTraceElement對android Log類進行簡單的封裝。可以打印出當前log在那個類,調用的方法名,行號。

/**
 * 
 */
package com.xx.market.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import android.util.Log;

public class LogUtility {
	public static boolean getDecideResult(StackTraceElement ele) {
		return true;
	}

	public static String getCallerInfo() {
		StackTraceElement[] stack = (new Throwable()).getStackTrace();
		StackTraceElement ele = null;
		String className = "";
		String methodName = "";
		int lineNO = 0;
		if (stack.length > 2) {
			ele = stack[2];
			try {
				className = Class.forName(ele.getClassName()).getSimpleName();
				methodName = ele.getMethodName();
				lineNO = ele.getLineNumber();
			} catch (ClassNotFoundException e) {
			}
		}

		String callerInfo = className + ":" + methodName + ":" + lineNO + "=>";
		return callerInfo;
	}

	public static void i(String tag, double info) {

		if (Constants.DEBUG) {

			Log.i(tag, getCallerInfo() + String.valueOf(info));
		}

	}

	public static void i(String tag, float info) {

		if (Constants.DEBUG) {
			Log.i(tag, getCallerInfo() + String.valueOf(info));
		}

	}

	public static void i(String tag, int info) {

		if (Constants.DEBUG) {
			Log.i(tag,getCallerInfo() + String.valueOf(info));
		}

	}

	public static void i(String tag, long info) {
		if (Constants.DEBUG) {

				Log.i(tag, getCallerInfo() + String.valueOf(info));

		}
	}

	public static void i(String tag, String info) {
		if (Constants.DEBUG) {

				Log.i(tag, getCallerInfo() + String.valueOf(info));

		}
	}

	public static void w(String tag, String info) {
		if (Constants.WARN) {

				Log.w(tag, getCallerInfo() + info);

		}
	}

	public static void e(String tag, String info) {
		if (Constants.ERROR) {

				Log.e(tag, getCallerInfo() + info);

		}
	}

	@SuppressWarnings("rawtypes")
	public static void i(String tag, String startMsg, Collection info) {
		if (Constants.DEBUG) {

			StringBuffer msg = null;
			if (startMsg == null) {
				msg = new StringBuffer(getCallerInfo());
			} else {
				msg = new StringBuffer(getCallerInfo() + startMsg);
			}
			Iterator itr = info.iterator();
			while (itr.hasNext()) {
				msg.append(itr.next().toString());
				msg.append(";");
			}

		Log.i(tag, msg.toString());

		}
	}

	@SuppressWarnings("rawtypes")
	public static void i(String tag, String startMsg, HashMap info) {
		if (Constants.DEBUG) {

			StringBuffer msg = null;
			if (startMsg == null) {
				msg = new StringBuffer(getCallerInfo());
			} else {
				msg = new StringBuffer(getCallerInfo() + startMsg);
			}
			Set keySet = info.keySet();
			Iterator itr = keySet.iterator();
			while (itr.hasNext()) {
				Object key = itr.next();
				Object value = info.get(key);
				msg.append(key.toString());
				msg.append("=");
				msg.append(value.toString());
				msg.append(";");
			}

			Log.i(tag, msg.toString());

		}
	}

	public static void debugForImage(String info) {
		// 正式環境去掉
		if (Constants.DEBUG) {
			Log.i("Market-lion", getCallerInfo() + info);
		}
	}

	public static void debug(String info) {
		// 正式環境去掉
		if (Constants.DEBUG) {
			Log.i("list",getCallerInfo() +  info);
		}
	}

}

 

 

 

發佈了89 篇原創文章 · 獲贊 2 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章