常用的Log日志打印与输出

/**
 * Log日志
 * 
 * @author Jenly
 * @date 2014-6-20
 */
public class LogUtils {

	public static final String TAG = "Jenly";

	private static final String COLON = ":";

	private static final String VERTICAL = "|";

	/** 是否显示Log日志 */
	private static boolean isShowLog = true;

	/** Log日志优先权 */
	private static int priority = 1;

	/**
	 * Priority constant for the println method;use System.out.println
	 */
	public static final int PRINTLN = 1;

	/**
	 * Priority constant for the println method; use Log.v.
	 */
	public static final int VERBOSE = 2;

	/**
	 * Priority constant for the println method; use Log.d.
	 */
	public static final int DEBUG = 3;

	/**
	 * Priority constant for the println method; use Log.i.
	 */
	public static final int INFO = 4;

	/**
	 * Priority constant for the println method; use Log.w.
	 */
	public static final int WARN = 5;

	/**
	 * Priority constant for the println method; use Log.e.
	 */
	public static final int ERROR = 6;

	/**
	 * Priority constant for the println method.use Log.wtf.
	 */
	public static final int ASSERT = 7;
	
	public static final String TAG_FORMAT = "%s.%s(L:%d)";

	public static void setShowLog(boolean isShowLog) {

		LogUtils.isShowLog = isShowLog;
	}

	public static boolean isShowLog() {

		return isShowLog;
	}

	public static int getPriority() {

		return priority;
	}

	public static void setPriority(int priority) {

		LogUtils.priority = priority;
	}

	/**
	 * 根据堆栈生成TAG
	 * @return TAG|className.methodName(L:lineNumber)
	 */
	private static String generateTag(StackTraceElement caller) {
		String tag = TAG_FORMAT;
		String callerClazzName = caller.getClassName();
		callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
		tag = String.format(tag,new Object[] { callerClazzName, caller.getMethodName(),Integer.valueOf(caller.getLineNumber()) });
		return new StringBuilder().append(TAG).append(VERTICAL).append(tag).toString();
	}

	/**
	 * 获取堆栈 
	 * @param n
	 * 		n=0		VMStack
	 * 		n=1		Thread		
	 * 		n=3		CurrentStack
	 * 		n=4		CallerStack
	 * 		...
	 * @return 	
	 */
	public static StackTraceElement getStackTraceElement(int n) {
		return Thread.currentThread().getStackTrace()[n];
	}
	
	/**
	 * 获取调用方的堆栈TAG
	 * @return
	 */
	private static String getCallerStackLogTag(){
		return generateTag(getStackTraceElement(5));
	}
	
	/**
	 * 
	 * @param t
	 * @return
	 */
	private static String getStackTraceString(Throwable t){
		return Log.getStackTraceString(t);
	}

	// -----------------------------------Log.v

	/**
	 * Log.v
	 * @param msg
	 */
	public static void v(String msg) {
		if (isShowLog && priority <= VERBOSE)
			Log.v(getCallerStackLogTag(), msg);
			
	}
	
	public static void v(Throwable t) {
		if (isShowLog && priority <= VERBOSE)
			Log.v(getCallerStackLogTag(), getStackTraceString(t));
	}
	
	public static void v(String msg,Throwable t) {
		if (isShowLog && priority <= VERBOSE)
			Log.v(getCallerStackLogTag(), msg, t);
	}
	
	// -----------------------------------Log.d
	
	/**
	 * Log.d
	 * @param msg
	 */
	public static void d(String msg) {
		if (isShowLog && priority <= DEBUG)
			Log.d(getCallerStackLogTag(), msg);
	}
	
	public static void d(Throwable t) {
		if (isShowLog && priority <= DEBUG)
			Log.d(getCallerStackLogTag(), getStackTraceString(t));
	}
	
	public static void d(String msg,Throwable t) {
		if (isShowLog && priority <= DEBUG)
			Log.d(getCallerStackLogTag(), msg, t);
	}
	
	// -----------------------------------Log.i
	
	/**
	 * Log.i
	 * @param msg
	 */
	public static void i(String msg) {
		if (isShowLog && priority <= INFO)
			Log.i(getCallerStackLogTag(), msg);
	}
	
	public static void i(Throwable t) {
		if (isShowLog && priority <= INFO)
			Log.i(getCallerStackLogTag(), getStackTraceString(t));
	}
	
	public static void i(String msg,Throwable t) {
		if (isShowLog && priority <= INFO)
			Log.i(getCallerStackLogTag(), msg, t);
	}
	
	// -----------------------------------Log.w
	
	/**
	 * Log.w
	 * @param msg
	 */
	public static void w(String msg) {
		if (isShowLog && priority <= WARN)
			Log.w(getCallerStackLogTag(), msg);
	}
	
	public static void w(Throwable t) {
		if (isShowLog && priority <= WARN)
			Log.w(getCallerStackLogTag(), getStackTraceString(t));
	}
	
	public static void w(String msg,Throwable t) {
		if (isShowLog && priority <= WARN)
			Log.w(getCallerStackLogTag(), msg, t);
	}
	
	// -----------------------------------Log.e
	
	/**
	 * Log.e
	 * @param msg
	 */
	public static void e(String msg) {
		if (isShowLog && priority <= ERROR)
			Log.e(getCallerStackLogTag(), msg);
	}
	
	public static void e(Throwable t) {
		if (isShowLog && priority <= ERROR)
			Log.e(getCallerStackLogTag(), getStackTraceString(t));
	}
	
	public static void e(String msg,Throwable t) {
		if (isShowLog && priority <= ERROR)
			Log.e(getCallerStackLogTag(), msg, t);
	}
	
	// -----------------------------------Log.wtf
	
	/**
	 * Log.wtf
	 * @param msg
	 */
	public static void wtf(String msg) {
		if (isShowLog && priority <= ASSERT)
			Log.wtf(getCallerStackLogTag(), msg);
	}
	
	public static void wtf(Throwable t) {
		if (isShowLog && priority <= ASSERT)
			Log.wtf(getCallerStackLogTag(), getStackTraceString(t));
	}
	
	public static void wtf(String msg,Throwable t) {
		if (isShowLog && priority <= ASSERT)
			Log.wtf(getCallerStackLogTag(), msg, t);
	}

	// -----------------------------------System.out.print

	/**
	 * System.out.print
	 * 
	 * @param msg
	 */
	public static void print(String msg) {
		if (isShowLog && priority <= PRINTLN)
			System.out.print(msg);
	}

	public static void print(Object obj) {
		if (isShowLog && priority <= PRINTLN)
			System.out.print(obj);
	}

	// -----------------------------------System.out.printf

	/**
	 * System.out.printf
	 * 
	 * @param msg
	 */
	public static void printf(String msg) {
		if (isShowLog && priority <= PRINTLN)
			System.out.printf(msg);
	}

	// -----------------------------------System.out.println

	/**
	 * System.out.println
	 * 
	 * @param msg
	 */
	public static void println(String msg) {
		if (isShowLog && priority <= PRINTLN)
			System.out.println(msg);
	}

	public static void println(Object obj) {
		if (isShowLog && priority <= PRINTLN)
			System.out.println(obj);
	}

}

只是进行了一个简单的封装、方便记录与管理、

转载于:https://www.cnblogs.com/jenly/p/5312374.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值