您的位置:首页 > 移动开发 > Android开发

【Android Util】全局控制Log打印日志

2015-07-18 11:30 519 查看
public class LogUtil {

private static final boolean DEBUG = true;

private LogUtil() {
throw new UnsupportedOperationException("Cannot initialize " + getClass().getCanonicalName() + " class");
}

private static String addCallerInformation() {
int i, lio; //lio = lastIndexOf
StackTraceElement stack[] = Thread.currentThread().getStackTrace();
for (i = 0; !stack[i].getClassName().equals(LogUtil.class.getName()); i++) {
}
for (; stack[i].getClassName().equals(LogUtil.class.getName()); i++) {
}
lio = stack[i].getFileName().lastIndexOf('.');
if (lio == -1) {
return " (" + stack[i].getFileName() + ":" + stack[i].getLineNumber() + ")";
} else {
return " (" + stack[i].getFileName().substring(0, lio) + ":" + stack[i].getLineNumber() + ")";
}
}

public static void i(Object obj, Object message){
if(DEBUG) {
if (obj != null && message != null) {
Log.i(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation());
}
}
}

public static void e(Object obj, Object message) {
if(DEBUG) {
if (obj != null && message != null) {
Log.e(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation());
}
}
}

public static void d(Object obj, Object message) {
if(DEBUG) {
if (obj != null && message != null) {
Log.d(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation());
}
}
}

public static void v(Object obj, Object message) {
if(DEBUG) {
if (obj != null && message != null) {
Log.v(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation());
}
}
}

public static void w(Object obj, Object message) {
if(DEBUG) {
if (obj != null && message != null) {
Log.w(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation());
}
}
}

public static void wtf(Object obj, Object message, Throwable t) {
if(DEBUG) {
if (obj != null && message != null) {
Log.wtf(obj instanceof CharSequence ? obj.toString() : obj.getClass().getSimpleName(), message.toString().trim() + addCallerInformation(), t);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: