LogUtil 控制全部log的显示和隐藏

public class LogUtil {
public static String TAG = "GooglaPlay/LogUtil";      这是过滤
public static boolean DEBUG = true;    //这是的true 是显示出来,flase是隐藏起来正式上线可以隐藏



/**
* Customize the log tag for your application<br />
* Enable the log property for your tag before starting your app: <br />
* {@code adb shell setprop log.tag.&lt;tag&gt;}
*/
public static void setTag(String tag) {
d("Changing log tag to %s", tag);
TAG = tag;
// Reinitialize the DEBUG "constant"
DEBUG = Log.isLoggable(TAG, Log.VERBOSE);
}


public static void v(String format, Object... args) {
if (DEBUG) {
Log.v(TAG, buildMessage(format, args));
}
}


public static void d(String format, Object... args) {
if (DEBUG)
Log.d(TAG, buildMessage(format, args));
}


public static void e(String format, Object... args) {
if (DEBUG)
Log.e(TAG, buildMessage(format, args));
}


public static void e(Throwable tr, String format, Object... args) {
if (DEBUG)
Log.e(TAG, buildMessage(format, args), tr);
}


public static void wtf(String format, Object... args) {
if (DEBUG)
Log.wtf(TAG, buildMessage(format, args));
}


public static void wtf(Throwable tr, String format, Object... args) {
if (DEBUG)
Log.wtf(TAG, buildMessage(format, args), tr);
}


/**
* Formats the caller's provided message and prepends useful info like
* calling thread ID and method name.
*/
private static String buildMessage(String format, Object... args) {
String msg = (args == null) ? format : String.format(Locale.US, format,
args);
StackTraceElement[] trace = new Throwable().fillInStackTrace()
.getStackTrace();
String caller = "<unknown>";
// It will be at least two frames up, so start there.
for (int i = 2; i < trace.length; i++) {
Class<?> clazz = trace[i].getClass();
if (!clazz.equals(LogUtil.class)) {
String callingClass = trace[i].getClassName();
callingClass = callingClass.substring(callingClass
.lastIndexOf('.') + 1);
callingClass = callingClass.substring(callingClass
.lastIndexOf('$') + 1);
caller = callingClass + "." + trace[i].getMethodName();
break;
}
}
return String.format(Locale.US, "[%d] %s: %s", Thread.currentThread()
.getId(), caller, msg);
}


/**
* A simple event log with records containing a name, thread ID, and
* timestamp.
*/
static class MarkerLog {
public static final boolean ENABLED = LogUtil.DEBUG;
/**
* Minimum duration from first marker to last in an marker log to
* warrant logging.
*/
private static final long MIN_DURATION_FOR_LOGGING_MS = 0;


private static class Marker {
public final String name;
public final long thread;
public final long time;


public Marker(String name, long thread, long time) {
this.name = name;
this.thread = thread;
this.time = time;
}
}


private final List<Marker> mMarkers = new ArrayList<Marker>();
private boolean mFinished = false;


/** Adds a marker to this log with the specified name. */
public synchronized void add(String name, long threadId) {
if (mFinished) {
throw new IllegalStateException("Marker added to finished log");
}
mMarkers.add(new Marker(name, threadId, SystemClock
.elapsedRealtime()));
}


/**
* Closes the log, dumping it to logcat if the time difference between
* the first and last markers is greater than
* {@link #MIN_DURATION_FOR_LOGGING_MS}.

* @param header
*            Header string to print above the marker log.
*/
public synchronized void finish(String header) {
mFinished = true;
long duration = getTotalDuration();
if (duration <= MIN_DURATION_FOR_LOGGING_MS) {
return;
}
long prevTime = mMarkers.get(0).time;
d("(%-4d ms) %s", duration, header);
for (Marker marker : mMarkers) {
long thisTime = marker.time;
d("(+%-4d) [%2d] %s", (thisTime - prevTime), marker.thread,
marker.name);
prevTime = thisTime;
}
}


@Override
protected void finalize() throws Throwable {
// Catch requests that have been collected (and hence end-of-lifed)
// but had no debugging output printed for them.
if (!mFinished) {
finish("Request on the loose");
e("Marker log finalized without finish() - uncaught exit point for request");
}
}


/**
* Returns the time difference between the first and last events in this
* log.
*/
private long getTotalDuration() {
if (mMarkers.size() == 0) {
return 0;
}
long first = mMarkers.get(0).time;
long last = mMarkers.get(mMarkers.size() - 1).time;
return last - first;
}
}

}


在代码中应用为:

       // 使用 Logutil %s %d
LogUtil.d("%s", "我是 debug日志,能看到我吗");
LogUtil.wtf("%s", "我是 wtf日志,能看到我吗");
LogUtil.e("%s", "我是error日志,能看到我吗");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值