首页 > 代码库 > 对于Android系统Log输出日志的封装LogUtils

对于Android系统Log输出日志的封装LogUtils

MainActivity如下:
package cc.cn.logutil;
import android.os.Bundle;
import android.app.Activity;
/**
 * Demo描述:
 * 对于Android系统Log输出日志的封装LogUtils
 * 1 可修改LogUtils中的LEVEL值觉得哪些级别的日志可以输出.
 *   所以方便选择性输出日志或者屏蔽日志输出
 * 2 输出的日志除了本想输出的信息外还包含了该日志输出时所属
 *   的线程,类,方法名,已经在该方法中的行数等实用信息
 * 3 LogUtils的调用方式
 *   3.1 按照系统原方式调用
 *   3.2 或者不设置TAG,则默认为文件名
 *   
 * 备注说明:
 * 在该示例中主要用到了StackTrace和StackTraceElement.
 * 详情可参见上篇博客《StackTrace简述以及StackTraceElement使用实例》
 * 
 * PS:
 * 今天有些感冒,有些发烧.下班后完成这个示例,算是一种慰藉吧
 * 2014年9月23日23:05:01.
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		LogUtils.i("MainActivity", "onCreate1");
		LogUtils.i(null, "onCreate2");
		LogUtils.i("", "onCreate3");
		LogUtils.i("onCreate4");
		test();
	}
	
	
	private void test() {
		LogUtils.i("调用了MainActivity中的test()");
		TestLog.testLog();
		new Thread() {
			public void run() {
				LogUtils.i("调用了子线程");
			};
		}.start();
	}

	@Override
	protected void onStart() {
		super.onStart();
		LogUtils.i("onStart");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		LogUtils.i("onResume");
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		LogUtils.i("onPause");
	}


	@Override
	protected void onStop() {
		super.onStop();
		LogUtils.i("onStop");
	}
	

	@Override
	protected void onDestroy() {
		super.onDestroy();
		LogUtils.i("onDestroy");
	}

	
}

LogUtils如下:
package cc.cn.logutil;

import android.text.TextUtils;
import android.util.Log;

/**
 * LogUtils工具说明: 
 * 1 只输出等级大于等于LEVEL的日志 
 *   所以在开发和产品发布后通过修改LEVEL来选择性输出日志.
 *   当LEVEL=NOTHING则屏蔽了所有的日志. 
 * 2 v,d,i,w,e均对应两个方法. 
 *   若不设置TAG或者TAG为空则为设置默认TAG
 * 
 */
public class LogUtils {
	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 final int NOTHING = 6;
	public static final int LEVEL = VERBOSE;
	public static final String SEPARATOR = ",";

	public static void v(String message) {
		if (LEVEL <= VERBOSE) {
			StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
			String tag = getDefaultTag(stackTraceElement);
			Log.v(tag, getLogInfo(stackTraceElement) + message);
		}
	}

	public static void v(String tag, String message) {
		if (LEVEL <= VERBOSE) {
			StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
			if (TextUtils.isEmpty(tag)) {
				tag = getDefaultTag(stackTraceElement);
			}
			Log.v(tag, getLogInfo(stackTraceElement) + message);
		}
	}

	public static void d(String message) {
		if (LEVEL <= DEBUG) {
			StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
			String tag = getDefaultTag(stackTraceElement);
			Log.d(tag, getLogInfo(stackTraceElement) + message);
		}
	}

	public static void d(String tag, String message) {
		if (LEVEL <= DEBUG) {
			StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
			if (TextUtils.isEmpty(tag)) {
				tag = getDefaultTag(stackTraceElement);
			}
			Log.d(tag, getLogInfo(stackTraceElement) + message);
		}
	}

	public static void i(String message) {
		if (LEVEL <= INFO) {
			StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
			String tag = getDefaultTag(stackTraceElement);
			Log.i(tag, getLogInfo(stackTraceElement) + message);
		}
	}

	public static void i(String tag, String message) {
		if (LEVEL <= INFO) {
			StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
			if (TextUtils.isEmpty(tag)) {
				tag = getDefaultTag(stackTraceElement);
			}
			Log.i(tag, getLogInfo(stackTraceElement) + message);
		}
	}

	public static void w(String message) {
		if (LEVEL <= WARN) {
			StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
			String tag = getDefaultTag(stackTraceElement);
			Log.w(tag, getLogInfo(stackTraceElement) + message);
		}
	}

	public static void w(String tag, String message) {
		if (LEVEL <= WARN) {
			StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
			if (TextUtils.isEmpty(tag)) {
				tag = getDefaultTag(stackTraceElement);
			}
			Log.w(tag, getLogInfo(stackTraceElement) + message);
		}
	}

	public static void e(String tag, String message) {
		if (LEVEL <= ERROR) {
			StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
			if (TextUtils.isEmpty(tag)) {
				tag = getDefaultTag(stackTraceElement);
			}
			Log.e(tag, getLogInfo(stackTraceElement) + message);
		}
	}

	/**
	 * 获取默认的TAG名称. 
	 * 比如在MainActivity.java中调用了日志输出. 
	 * 则TAG为MainActivity
	 */
	public static String getDefaultTag(StackTraceElement stackTraceElement) {
		String fileName = stackTraceElement.getFileName();
		String stringArray[] = fileName.split("\\.");
		String tag = stringArray[0];
		return tag;
	}

	/**
	 * 输出日志所包含的信息
	 */
	public static String getLogInfo(StackTraceElement stackTraceElement) {
		StringBuilder logInfoStringBuilder = new StringBuilder();
		// 获取线程名
		String threadName = Thread.currentThread().getName();
		// 获取线程ID
		long threadID = Thread.currentThread().getId();
		// 获取文件名.即xxx.java
		String fileName = stackTraceElement.getFileName();
		// 获取类名.即包名+类名
		String className = stackTraceElement.getClassName();
		// 获取方法名称
		String methodName = stackTraceElement.getMethodName();
		// 获取生日输出行数
		int lineNumber = stackTraceElement.getLineNumber();

		logInfoStringBuilder.append("[ ");
		logInfoStringBuilder.append("threadID=" + threadID).append(SEPARATOR);
		logInfoStringBuilder.append("threadName=" + threadName).append(SEPARATOR);
		logInfoStringBuilder.append("fileName=" + fileName).append(SEPARATOR);
		logInfoStringBuilder.append("className=" + className).append(SEPARATOR);
		logInfoStringBuilder.append("methodName=" + methodName).append(SEPARATOR);
		logInfoStringBuilder.append("lineNumber=" + lineNumber);
		logInfoStringBuilder.append(" ] ");
		return logInfoStringBuilder.toString();
	}

}

TestLog如下:
package cc.cn.logutil;

public class TestLog {
     public static void testLog(){
    	 LogUtils.i("调用了TestLog中的testLog()");
     }
}

main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="对于Android系统Log输出日志的封装LogUtils" 
        android:layout_centerInParent="true"/>

</RelativeLayout>


对于Android系统Log输出日志的封装LogUtils