定义自己的Log管理类

来源:互联网 发布:看epub的软件 编辑:程序博客网 时间:2024/05/04 16:16

在看别人写的SDK的时候,发现他们对Log的管理是非常好的,在Android SDK的Log类的基础上进行了一层封装,具体的做法如下代码:

package com.example.adaptertest;import android.util.Log;public class LogUtil {/** default value is true */private static boolean debug = true ;public static void enableLog(){debug = true ;}public static void disableLog(){debug = false ;}public static void e(String tag ,String msg){if(debug){Log.e(tag ,msg);}}public static void i(String tag ,String msg){if(debug){Log.i(tag ,msg);}}public static void d(String tag ,String msg){if(debug){Log.e(tag ,msg);}}public static void w(String tag ,String msg){if(debug){Log.w(tag ,msg);}}public static void v(String tag ,String msg){if(debug){Log.v(tag ,msg);}}public static String  getStackTraceMsg(){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;}return "[" + Thread.currentThread().getName() + "(" + Thread.currentThread().getId()+ "): " + st.getFileName() + ":" + st.getLineNumber() + "]";}return null;} }
这样做对整个SDK的打印有着非常好的管理作用,但是这需要提出的是eableLog 和disableLog不要随意调用,建议只在主类中调用,不然容易引起混乱。

0 0
原创粉丝点击