android的Log日志打印管理工具类(一)
android的Log日志的打印管理工具类:
1 package com.gzcivil.utils; 2 3 import android.util.Log; 4 5 /** 6 * 日志打印管理 7 * 8 * @author LiJinlun 9 * @time 2015-12-15 10 */ 11 public class LogUtils { 12 private static LogType mLogType; 13 private static String mDefaultPrefix = " --- "; 14 15 static { 16 mLogType = LogType.ALL; 17 } 18 19 /** 20 * 日志类型 21 */ 22 public enum LogType { 23 NONE, VERBOSE, DEBUG, INFO, WARN, ERROR, ALL 24 } 25 26 /** 27 * 获取日志输出类型 28 * 29 * @return 30 */ 31 public static LogType getLogType() { 32 return mLogType; 33 } 34 35 /** 36 * 设置日志输出类型 37 * 38 * @param mLogType 39 */ 40 public static void setLogType(LogType mLogType) { 41 LogUtils.mLogType = mLogType; 42 } 43 44 public static final void e(String tag, Object... objects) { 45 if (logable(LogType.ERROR)) { 46 StrBuffer sb = new StrBuffer(); 47 sb.append(mDefaultPrefix); 48 sb.append(objects); 49 Log.e(tag, sb.toString()); 50 } 51 } 52 53 public static final String getExceptionInfo(Throwable tr) { 54 return Log.getStackTraceString(tr); 55 } 56 57 public static final void w(String tag, Object... objects) { 58 if (logable(LogType.WARN)) { 59 StrBuffer sb = new StrBuffer(); 60 sb.append(mDefaultPrefix); 61 sb.append(objects); 62 Log.w(tag, sb.toString()); 63 } 64 } 65 66 public static final void i(String tag, Object... objects) { 67 if (logable(LogType.INFO)) { 68 StrBuffer sb = new StrBuffer(); 69 sb.append(mDefaultPrefix); 70 sb.append(objects); 71 Log.i(tag, sb.toString()); 72 } 73 } 74 75 public static final void d(String tag, Object... objects) { 76 if (logable(LogType.DEBUG)) { 77 StrBuffer sb = new StrBuffer(); 78 sb.append(mDefaultPrefix); 79 sb.append(objects); 80 Log.d(tag, sb.toString()); 81 } 82 } 83 84 public static final void v(String tag, Object... objects) { 85 if (logable(LogType.VERBOSE)) { 86 StrBuffer sb = new StrBuffer(); 87 sb.append(mDefaultPrefix); 88 sb.append(objects); 89 Log.v(tag, sb.toString()); 90 } 91 } 92 93 private static final boolean logable(LogType aLogType) { 94 if (mLogType == LogType.ALL || mLogType == aLogType) { 95 return true; 96 } 97 return false; 98 } 99 100 /** 101 * debug标签日志输出(建议仅在测试时使用改方法) 102 * 103 * @param objects 104 */ 105 public static final void debug(Object... objects) { 106 if (logable(LogType.DEBUG)) { 107 StrBuffer sb = new StrBuffer(); 108 sb.append(mDefaultPrefix); 109 sb.append(objects); 110 Log.d("debug", sb.toString()); 111 } 112 } 113 114 }