Android 自定义Log

本文介绍了一种用于Android应用程序的日志记录工具类实现。该工具类提供了多种级别的日志记录方法,如DEBUG、INFO、WARN、ERROR等,并支持传递多个参数进行格式化输出。此外,还介绍了如何获取当前调用堆栈信息并将其用于日志记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
		}
	}
	

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值