让android日志更有用些

来源:互联网 发布:苹果手机使用软件 编辑:程序博客网 时间:2024/05/21 07:46

原文地址: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);}}}

 

 

 

原创粉丝点击