Android的Log等级介绍以及自定义Log工具类

Android1年前 (2022)更新 125la.com@站长
0 0
125啦读书导航,我的单页导航

 

一:Log等级介绍

Android的Log等级通常有五类,按照日志级别由低到高分别是Verbose、Debug、Info、Warning、Error,其对应的log定义在system层。
1.V
Verbose就是冗长啰嗦的。通常表达开发调试过程中的一些详细信息,用Log.v()输出,不过滤地输出所有调试信息。是最低级的Log可以不用管。
2.D
Debug来表达调试信息。用Log.d()输出,能输出Debug、Info、Warning、Error级别的Log信息。
3.I
Info来表达一些信息。用Log.i()输出,能输出Info、Warning、Error级别的Log信息。
4.W
Warning表示警告,但不一定会马上出现错误,开发时有时用来表示特别注意的地方。用Log.w()输出,能输出Warning、Error级别的Log信息。
5.E
Error表示出现错误,是最需要关注解决的。用Log.e()输出,能输出Error级别的Log信息。
注:Info、Warnning、Error等级的Log在普通调试中不随意滥用,存在发布版本中。在开发调试版本中,才会显示全部等级。
以上内容摘抄于《Android的Log等级

二:自定义log工具类

示例代码一

package com.biumall
.utils;

import android.util.Log;

public class LogUtils {

	// constant value
	public static final int LOG_LEVEL_V = 5;
	public static final int LOG_LEVEL_D = 4;
	public static final int LOG_LEVEL_I = 3;
	public static final int LOG_LEVEL_W = 2;
	public static final int LOG_LEVEL_E = 1;

	public static int log_level = LOG_LEVEL_V; // default log level

	public static String TAG = "www.125la.com"; //

	public static void v(String msg) {
		if (log_level >= LOG_LEVEL_V) {
			Log.d(TAG, msg);
		}
		return;
	}

	public static void v(String tag, String msg) {
		if (log_level >= LOG_LEVEL_V) {
			Log.d(tag, msg);
		}
		return;
	}

	public static void d(String msg) {
		if (log_level >= LOG_LEVEL_D) {
			Log.d(TAG, msg);
		}
		return;
	}

	public static void d(String tag, String msg) {
		if (log_level >= LOG_LEVEL_D) {
			Log.d(tag, msg);
		}
		return;
	}

	public static void i(String msg) {
		if (log_level >= LOG_LEVEL_I) {
			Log.d(TAG, msg);
		}
		return;
	}

	public static void i(String tag, String msg) {
		if (log_level >= LOG_LEVEL_I) {
			Log.d(tag, msg);
		}

		return;
	}

	public static void w(String msg) {
		if (log_level >= LOG_LEVEL_W) {
			Log.d(TAG, msg);
		}
		return;
	}

	public static void w(String tag, String msg) {
		if (log_level >= LOG_LEVEL_W) {
			Log.d(tag, msg);
		}

		return;
	}

	public static void e(String msg) {
		if (log_level >= LOG_LEVEL_E) {
			Log.d(TAG, msg);
		}
		return;
	}

	public static void e(String tag, String msg) {
		if (log_level >= LOG_LEVEL_E) {
			Log.d(tag, msg);
		}

		return;
	}
}

示例代码二

package com.biumall.utils;

import android.util.Log;

public class LogUtils {

	public static boolean isDebug = true; // default debug model true
	public static String TAG = "www.l25la.com";// default tag

	public static void d(String msg) {
		if (isDebug) {
			Log.d(TAG, msg);
		}
		return;
	}

	public static void d(String tag, String msg) {
		if (isDebug) {
			Log.d(tag, msg);
		}
		return;
	}

	public static void e(String msg) {
		if (isDebug) {
			Log.e(TAG, msg);
		}
		return;
	}

	public static void e(String tag, String msg) {
		if (isDebug) {
			Log.e(tag, msg);
		}
		return;
	}

	public static void v(String msg) {
		if (isDebug) {
			Log.v(TAG, msg);
		}
		return;
	}

	public static void v(String tag, String msg) {
		if (isDebug) {
			Log.v(tag, msg);
		}
		return;
	}

	public static void w(String msg) {
		if (isDebug) {
			Log.w(TAG, msg);
		}
		return;
	}

	public static void w(String tag, String msg) {
		if (isDebug) {
			Log.w(tag, msg);
		}
		return;
	}

	public static void i(String msg) {
		if (isDebug) {
			Log.i(TAG, msg);
		}
		return;
	}

	public static void i(String tag, String msg) {
		if (isDebug) {
			Log.i(tag, msg);
		}
		return;
	}
}

个人偏向于代码一,可以通过level调节日志的输出,当然代码示例二也用。

© 版权声明
125啦读书导航,我的单页导航

相关文章

125啦读书导航,我的单页导航

暂无评论

暂无评论...