Android Logcat日志优化

在Android开发里,logcat日志是常用的打印调试功能。不过,Android提供的logcat类仅能打印TAG信息和content。受C语言宏定义__FILE__及__FUNCTION__启发,可对logcat日志输出进行优化。

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

      在android开发中,logcat日志是最常用的打印调试功能。但是android提供的logcat类只能打印TAG信息以及content,因此,受C语言宏定义__FILE__以及__FUNCTION__的启发,优化logcat日志输出。

import android.util.Log;

public class LogcatUtils {
	private static final String TAG = "TAG";
	public static final int VERBOSE = 1;
	public static final int DEBUG = 2;
	public static final int INFO = 3;
	public static final int WARN = 4;
	public static final int ERROR = 5;
	public static int LEVEL = ERROR;
	public static final String SEPARATOR = " -> ";
	public static boolean isDebug = true;

	public static void v(String message) {
		if (LEVEL >= VERBOSE && isDebug)
			Log.v(TAG, getLogInfo() + message);
	}

	public static void d(String message) {
		if (LEVEL >= DEBUG && isDebug)
			Log.d(TAG, getLogInfo() + message);
	}

	public static void i(String message) {
		if (LEVEL >= INFO && isDebug)
			Log.i(TAG, getLogInfo() + message);
	}

	public static void w(String message) {
		if (LEVEL >= WARN && isDebug)
			Log.w(TAG, getLogInfo() + message);
	}

	public static void e(String message) {
		if (LEVEL >= ERROR && isDebug)
			Log.e(TAG, getLogInfo() + message);
	}

	/**
	 * 输出日志所包含的信息
	 */
	private static String getLogInfo() {
		StackTraceElement traceElement = ((new Exception()).getStackTrace())[2];
		StringBuffer toStringBuffer = new StringBuffer().append("[").append(traceElement.getFileName())
				.append("-> Method:").append(traceElement.getMethodName()).append(" -> Line:")
				.append(traceElement.getLineNumber()).append("]");
		return toStringBuffer.toString();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值