Android初学之------Log 工具类,方便查看打印log的位置 类名信息

引用至

http://angrycode.cn/archives/145


import android.util.Log;

public class Logger {
	/**
	 * log tag
	 */
	private String tag = "Logger";//application name
	/**
	 * debug or not
	 */
	private static boolean debug = true;
 
	private static Logger instance = new Logger();
 
	private Logger() {
 
	}
 
	public static Logger getLogger() {
		return instance;
	}
 
	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().getName() + "(" + Thread.currentThread().getId()
					+ "): " + st.getFileName() + ":" + st.getLineNumber() + "]";
		}
 
		return null;
	}
 
	private String createMessage(String msg) {
		String functionName = getFunctionName();
		String message = (functionName == null ? msg : (functionName + " - " + msg));
		return message;
	}
 
	/**
	 * log.i
	 */
	public void i(String msg) {
		if (debug) {
			String message = createMessage(msg);
			Log.i(tag, message);
		}
	}
 
	/**
	 * log.v
	 */
	public void v(String msg) {
		if (debug) {
			String message = createMessage(msg);
			Log.v(tag, message);
		}
	}
 
	/**
	 * log.d
	 */
	public void d(String msg) {
		if (debug) {
			String message = createMessage(msg);
			Log.d(tag, message);
		}
	}
 
	/**
	 * log.e
	 */
	public void e(String msg) {
		if (debug) {
			String message = createMessage(msg);
			Log.e(tag, message);
		}
	}
	/**
	 * log.error 
	 */
	public void error(Exception e){
		if(debug){
			StringBuffer sb = new StringBuffer();
	        String name = getFunctionName();
	        StackTraceElement[] sts = e.getStackTrace();
 
	        if (name != null) {
                sb.append(name+" - "+e+"\r\n");
            } else {
                sb.append(e+"\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());
		}
	}
	/**
	 * log.d
	 */
	public void w(String msg) {
		if (debug) {
			String message = createMessage(msg);
			Log.w(tag, message);
		}
	}
 
	public void setTag(String tag) {
		this.tag = tag;
	}
 
	/**
	 * set debug
	 */
	public static void setDebug(boolean d) {
		debug = d;
	}
}
使用方法
Logger mLogger = Logger.getLogger();
mLogger.d("test logger");



在贴一个  

import android.util.Log;

public class Logout
{
    private static boolean LOGGING_ENABLED;

    private static final int STACK_TRACE_LEVELS_UP = 5;

    public static void verbose(String tag, String message)
    {
	if (LOGGING_ENABLED)
	{
            Log.v(tag, getClassNameMethodNameAndLineNumber() + message);
	}
    }

    /**
     * Get the current line number. Note, this will only work as called from
     * this class as it has to go a predetermined number of steps up the stack
     * trace. In this case 5.
     * 
     * @author kvarela
     * @return int - Current line number.
     */
    private static int getLineNumber()
    {
	return Thread.currentThread().getStackTrace()[STACK_TRACE_LEVELS_UP].getLineNumber();
    }

    /**
     * Get the current class name. Note, this will only work as called from this
     * class as it has to go a predetermined number of steps up the stack trace.
     * In this case 5.
     * 
     * @author kvarela
     * @return String - Current line number.
     */
    private static String getClassName()
    {
	String fileName = Thread.currentThread().getStackTrace()[STACK_TRACE_LEVELS_UP].getFileName();

	// kvarela: Removing ".java" and returning class name
	return fileName.substring(0, fileName.length() - 5);
    }

    /**
     * Get the current method name. Note, this will only work as called from
     * this class as it has to go a predetermined number of steps up the stack
     * trace. In this case 5.
     * 
     * @author kvarela
     * @return String - Current line number.
     */
    private static String getMethodName()
    {
	return Thread.currentThread().getStackTrace()[STACK_TRACE_LEVELS_UP].getMethodName();
    }

    /**
     * Returns the class name, method name, and line number from the currently
     * executing log call in the form <class_name>.<method_name>()-<line_number>
     * 
     * @author kvarela
     * @return String - String representing class name, method name, and line
     *         number.
     */
    private static String getClassNameMethodNameAndLineNumber()
    {
	return "[" + getClassName() + "." + getMethodName() + "()-" + getLineNumber() + "]: ";
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值