Android 自定义Log

来源:互联网 发布:mac上其他怎么删除 编辑:程序博客网 时间:2024/05/16 15:47
import static android.util.Log.DEBUG;import static android.util.Log.ERROR;import static android.util.Log.INFO;import static android.util.Log.WARN;/** * Convenient wrapper for Android logs. * * @author Guillaume Beraudo */public final class Log {public static final String TAG = "LogDemo";private static final boolean useIsLoggable = false;@SuppressWarnings(value="all")private static boolean isLoggable(int level) {return !useIsLoggable || android.util.Log.isLoggable(TAG, level);}public static void i(Object...objects) {if (isLoggable(INFO)) {android.util.Log.i(TAG, toString(objects));}}public static void i(Throwable t, Object...objects) {if (isLoggable(INFO)) {android.util.Log.i(TAG, toString(objects), t);}}public static void d(Object...objects) {if (isLoggable(DEBUG)) {android.util.Log.d(TAG, toString(objects));}}public static void d(Throwable t, Object...objects) {if (isLoggable(DEBUG)) {android.util.Log.d(TAG, toString(objects), t);}}public static void w(Object...objects) {if (isLoggable(WARN)) {android.util.Log.w(TAG, toString(objects));}}public static void w(Throwable t, Object...objects) {if (isLoggable(WARN)) {android.util.Log.w(TAG, toString(objects), t);}}public static void e(Object...objects) {if (isLoggable(ERROR)) {android.util.Log.e(TAG, toString(objects));}}public static void e(Throwable t, Object...objects) {if (isLoggable(ERROR)) {android.util.Log.e(TAG, toString(objects), t);}}/** * @throws RuntimeException always throw after logging the error message. */public static void f(Object...objects) {if (isLoggable(ERROR)) {android.util.Log.e(TAG, toString(objects));throw new RuntimeException("Fatal error : " + toString(objects));}}/** * @throws RuntimeException always throw after logging the error message. */public static void f(Throwable t, Object...objects) {if (isLoggable(ERROR)) {android.util.Log.e(TAG, toString(objects), t);throw new RuntimeException("Fatal error : " + toString(objects), t);}}private static String toString(Object...objects) {StringBuilder sb = new StringBuilder();for (Object o : objects) {sb.append(o);}return sb.toString();}}


public final class Log {private static boolean sIsLogEnabled = true;// 是否打开LOGprivate static String sApplicationTag = "LogDemo";// LOG默认TAGprivate static final String TAG_CONTENT_PRINT = "%s:%s.%s:%d";private static StackTraceElement getCurrentStackTraceElement() {return Thread.currentThread().getStackTrace()[4];}//打印LOGpublic static void trace() {if (sIsLogEnabled) {android.util.Log.d(sApplicationTag,getContent(getCurrentStackTraceElement()));}}//获取LOGprivate static String getContent(StackTraceElement trace) {return String.format(TAG_CONTENT_PRINT, sApplicationTag,trace.getClassName(), trace.getMethodName(),trace.getLineNumber());}//打印默认TAG的LOGpublic static void traceStack() {if (sIsLogEnabled) {traceStack(sApplicationTag, android.util.Log.ERROR);}}// 打印Log当前调用栈信息public static void traceStack(String tag, int priority) {if (sIsLogEnabled) {StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();android.util.Log.println(priority, tag, stackTrace[4].toString());StringBuilder str = new StringBuilder();String prevClass = null;for (int i = 5; i < stackTrace.length; i++) {String className = stackTrace[i].getFileName();int idx = className.indexOf(".java");if (idx >= 0) {className = className.substring(0, idx);}if (prevClass == null || !prevClass.equals(className)) {str.append(className.substring(0, idx));}prevClass = className;str.append(".").append(stackTrace[i].getMethodName()).append(":").append(stackTrace[i].getLineNumber()).append("->");}android.util.Log.println(priority, tag, str.toString());}}//指定TAG和指定内容的方法public static void d(String tag, String msg) {if (sIsLogEnabled) {android.util.Log.d(tag, getContent(getCurrentStackTraceElement())+">"+msg);}}//默认TAG和制定内容的方法public static void d(String msg) {if (sIsLogEnabled) {android.util.Log.d(sApplicationTag, getContent(getCurrentStackTraceElement())+">"+msg);}}//不同级别的Logpublic static void i(String tag,String msg){if (sIsLogEnabled) {android.util.Log.d(sApplicationTag, getContent(getCurrentStackTraceElement())+">"+msg);}}public static void w(String tag,String msg){if (sIsLogEnabled) {android.util.Log.d(sApplicationTag, getContent(getCurrentStackTraceElement())+">"+msg);}}public static void e(String tag,String msg){if (sIsLogEnabled) {android.util.Log.d(sApplicationTag, getContent(getCurrentStackTraceElement())+">"+msg);}}}





原创粉丝点击