- import android.util.Log;
- public class LogUtil {
- private static boolean DEBUG = true;
- private static String tag = "[app]";
- private String mClassName;
- private static LogUtil log;
- private static final String USER_NAME = "@user@";
- private LogUtil(String name) {
- mClassName = name;
- }
- /**
- * Get The Current Function Name
- *
- * @return Name
- */
- private String getFunctionName() {
- StackTraceElement[] sts = Thread.currentThread().getStackTrace();
- if (sts == null) {
- return null;
- }
- for (StackTraceElement st : sts) {
- if (st.isNativeMethod()) {
- continue;
- }
- if (st.getClassName().equals(Thread.class.getName())) {
- continue;
- }
- if (st.getClassName().equals(this.getClass().getName())) {
- continue;
- }
- return mClassName + "[ " + Thread.currentThread().getName() + ": "
- + st.getFileName() + ":" + st.getLineNumber() + " "
- + st.getMethodName() + " ]";
- }
- return null;
- }
- public static void i(Object str) {
- print(Log.INFO, str);
- }
- public static void d(Object str) {
- print(Log.DEBUG, str);
- }
- public static void v(Object str) {
- print(Log.VERBOSE, str);
- }
- public static void w(Object str) {
- print(Log.WARN, str);
- }
- public static void e(Object str) {
- print(Log.ERROR, str);
- }
- private static void print(int index, Object str) {
- if (!DEBUG) {
- return;
- }
- if (log == null) {
- log = new LogUtil(USER_NAME);
- }
- String name = log.getFunctionName();
- if (name != null) {
- str = name + " - " + str;
- }
- switch (index) {
- case Log.VERBOSE:
- Log.v(tag, str.toString());
- break;
- case Log.DEBUG:
- Log.d(tag, str.toString());
- break;
- case Log.INFO:
- Log.i(tag, str.toString());
- break;
- case Log.WARN:
- Log.w(tag, str.toString());
- break;
- case Log.ERROR:
- Log.e(tag, str.toString());
- break;
- default:
- break;
- }
- }
- }
直接作为工具类使用 调用非常方便 默认是debug模式。发布时候 直接把变量DEBUG改成false就ok 也可以方便把不同测试等级与debug 联动
LogUtil.i("测=i====count= "+count++);
LogUtil.e("测=e====count= "+count++);