Android Log日志规则打印

现有好多项目日志太乱。发布的版本日志依据存在。通篇的sysotem.out.print

现有日志类特点
 
/**
 * Log工具,类似android.util.Log。 tag自动产生,格式:
 * customTagPrefix:className.methodName(L:lineNumber),
 * customTagPrefix为空时只输出:className.methodName(L:lineNumber)。
 * 
 */

可以准确的定位到类。哪一行输出的日志
logutilConfig 控制是否打印日志

1、LogUtil.class

public class LogUtil {

	public static String customTagPrefix = "Sunday";

	private LogUtil() {
	}

	private static String generateTag() {
		StackTraceElement caller = new Throwable().getStackTrace()[2];
		String tag = "%s.%s(L:%d)";
		String callerClazzName = caller.getClassName();
		callerClazzName = callerClazzName.substring(callerClazzName
				.lastIndexOf(".") + 1);
		tag = String.format(tag, callerClazzName, caller.getMethodName(),
				caller.getLineNumber());
		tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":"
				+ tag;
		return tag;
	}

	public static void d(String content) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.d(tag, content);
	}

	public static void d(String content, Throwable tr) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.d(tag, content, tr);
	}

	public static void e(String content) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.e(tag, content);
	}

	public static void e(String content, Throwable tr) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.e(tag, content, tr);
	}

	public static void i(String content) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.i(tag, content);
	}

	public static void i(String content, Throwable tr) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.i(tag, content, tr);
	}

	public static void v(String content) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.v(tag, content);
	}

	public static void v(String content, Throwable tr) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.v(tag, content, tr);
	}

	public static void w(String content) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.w(tag, content);
	}

	public static void w(String content, Throwable tr) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.w(tag, content, tr);
	}

	public static void w(Throwable tr) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.w(tag, tr);
	}

	public static void wtf(String content) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.wtf(tag, content);
	}

	public static void wtf(String content, Throwable tr) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.wtf(tag, content, tr);
	}

	public static void wtf(Throwable tr) {
		if (!LogConfig.isDebug)
			return;
		String tag = generateTag();

		Log.wtf(tag, tr);
	}

}
2、LogConfig.class(主要配置LogUtil是否输出日志)

public class LogConfig {

	public static boolean isDebug = true;
}



你可能感兴趣的:(log)