|
package com.electric.chargingpile.util;
import android.util.Log;
/**
* @author tanlifei
* @ClassName: LogUtil
* @Description: 统一输出日志
* @date 2015-01-26 下午11:28:55
*/
public class LogUtils {
private final static String TAG = "LogUtils";
public static int logLevel = Log.VERBOSE;// 可以更改此值来过滤输出
private static LogUtils logger = new LogUtils();
;
private static boolean debug = true;
public static LogUtils getLogger() {
return logger;
}
private LogUtils() {
}
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 "[ " + Thread.currentThread().getName() + ": "
+ st.getFileName() + ":" + st.getLineNumber() + " ]";
}
return null;
}
public void i(Object str) {
if (!debug)
return;
if (logLevel <= Log.INFO) {
String name = getFunctionName();
if (name != null) {
Log.i(TAG, name + " - " + str);
} else {
Log.i(TAG, str.toString());
}
}
}
public void i(String TAG, Object str) {
if (!debug)
return;
if (logLevel <= Log.INFO) {
String name = getFunctionName();
if (name != null) {
Log.i(TAG, name + " - " + str);
} else {
Log.i(TAG, str.toString());
}
}
}
public void v(Object str) {
if (!debug)
return;
if (logLevel <= Log.VERBOSE) {
String name = getFunctionName();
if (name != null) {
Log.v(TAG, name + " - " + str);
} else {
Log.v(TAG, str.toString());
}
}
}
public void v(String TAG, Object str) {
if (!debug)
return;
if (logLevel <= Log.VERBOSE) {
String name = getFunctionName();
if (name != null) {
Log.v(TAG, name + " - " + str);
} else {
Log.v(TAG, str.toString());
}
}
}
public void w(Object str) {
if (!debug)
return;
if (logLevel <= Log.WARN) {
String name = getFunctionName();
if (name != null) {
Log.w(TAG, name + " - " + str);
} else {
Log.w(TAG, str.toString());
}
}
}
public void w(String TAG, Object str) {
if (!debug)
return;
if (logLevel <= Log.WARN) {
String name = getFunctionName();
if (name != null) {
Log.w(TAG, name + " - " + str);
} else {
Log.w(TAG, str.toString());
}
}
}
public void e(Object str) {
if (!debug)
return;
if (logLevel <= Log.ERROR) {
String name = getFunctionName();
if (name != null) {
Log.e(TAG, name + " - " + str);
} else {
Log.e(TAG, str.toString());
}
}
}
public void e(String TAG, Object str) {
if (!debug)
return;
if (logLevel <= Log.ERROR) {
String name = getFunctionName();
if (name != null) {
Log.e(TAG, name + " - " + str);
} else {
Log.e(TAG, str.toString());
}
}
}
public void e(Exception e) {
if (!debug)
return;
if (logLevel <= Log.ERROR) {
Log.e(TAG, "error", e);
}
}
public void e(String TAG, Exception e) {
if (!debug)
return;
if (logLevel <= Log.ERROR) {
Log.e(TAG, "error", e);
}
}
public void d(Object str) {
if (!debug)
return;
if (logLevel <= Log.DEBUG) {
String name = getFunctionName();
if (name != null) {
Log.d(TAG, name + " - " + str);
} else {
Log.d(TAG, str.toString());
}
}
}
public void d(String TAG, Object str) {
if (!debug)
return;
if (logLevel <= Log.DEBUG) {
String name = getFunctionName();
if (name != null) {
Log.d(TAG, name + " - " + str);
} else {
Log.d(TAG, str.toString());
}
}
}
}
|