Android Log工具類

=============================================java

博客原文地址:http://ihongqiqu.com/2014/10/16/android-log/android

==========================================git

工欲善其事,必先利其器。在Android開發過程當中,相信都會遇到這樣的問題:github

  • Log太多,不容易找到本身設置打印的內容
  • 找到Log可是定位到Java源碼有不是很方便
  • 對Log打印須要設置Tag,大多時候Tag就是本類的類名,存在重複的工做
  • 發佈正式版本須要註釋或者關掉不少Log開關

參考Android源碼,使用官方的Log打印方法(設置Tag),而後再Eclipse裏面設置過濾標籤,這樣就能夠查看某一個類的Log了,可是這樣效率比較低下。基於上面的一些問題,我研究實現了一種簡單易用的Log工具類。個人工具類主要有以下特色:express

  • 減小一個Log參數
  • 調用某些Log方法,能夠實現跳轉到Java源代碼的功能
  • 採用此工具類,屏蔽Log只須要關閉Log總開關便可

減小一個Log參數

實現方法是採用 StackTraceElement 獲取調用類的方法,代碼以下:apache

1
2 3 4 5 6 
private static String getClassName() {  String result = "";  StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[2];  result = thisMethodStack.getClassName();  return result; } 

跳轉到源代碼Log調用處

實現雙擊Log跳轉到Java源代碼調用處,實現方法是參考Android Logcat錯處日誌,發現Eclipse實現跳轉是由於Log中有 at MyClassName.MyMethodName(MClassName.java:行數) 。具體見代碼:app

1
2 3 4 5 6 7 8 9 10 11 12 13 14 
/**  * Realization of double click jump events.  *  * @return  */ private static String callMethodAndLine() {  String result = "at ";  StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[2];  result += thisMethodStack.getClassName() + ".";  result += thisMethodStack.getMethodName();  result += "(" + thisMethodStack.getFileName();  result += ":" + thisMethodStack.getLineNumber() + ") ";  return result; } 

屏蔽全部Log

採用 LogUtils 工具類,只須要將此類中DEBUG_LEVEL設置的值大一點。less

  • 若是想要屏蔽全部的Log只須要將 DEBUG_LEVEL 設置爲 Log.ASSERT 便可。
  • 若是想要只顯示Error則只須要將 DEBUG_LEVEL 設置爲 Log.WARN 便可。
  • 若是隻想要顯示Error和Warn則將 DEBUG_LEVEL 設置爲 Log.INFO 便可。

使用方法

只須要用LogUtils替換掉Android系統的Log類,並去掉log方法的第一個參數,例如:ide

1
LogUtils.d(Object); // 次行代碼將打印Object類的toString() 

無代碼無真相,下面是此工具類源碼:工具

1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 
/**  * Copyright 2014 Zhenguo Jin  *  * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance with the License.  * You may obtain a copy of the License at  *  * http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */ package com.ihongqiqu.util;  import android.util.Log;  /**  * This class can replace android.util.Log.  *  * @author jingle1267@163.com  *  * @description And you can turn off the log by set DEBUG_LEVEL = Log.ASSERT.  *  */ public class LogUtils {   /**  * Master switch.To catch error info you need set this value below Log.WARN  */  public static final int DEBUG_LEVEL = 0;   /**  * 'System.out' switch.When it is true, you can see the 'System.out' log.  * Otherwise, you cannot.  */  public static final boolean DEBUG_SYSOUT = false;   /**  * Send a {@link #VERBOSE} log message.  *  * @param obj  */  public static void v(Object obj) {  if (Log.VERBOSE > DEBUG_LEVEL) {  String tag = getClassName();  String msg = obj != null ? obj.toString() : "obj == null";  Log.v(tag, msg);  }  }   /**  * Send a {@link #DEBUG_LEVEL} log message.  *  * @param obj  */  public static void d(Object obj) {  if (Log.DEBUG > DEBUG_LEVEL) {  String tag = getClassName();  String msg = obj != null ? obj.toString() : "obj == null";  Log.d(tag, msg);  }  }   /**  * Send an {@link #INFO} log message.  *  * @param obj  */  public static void i(Object obj) {  if (Log.INFO > DEBUG_LEVEL) {  String tag = getClassName();  String msg = obj != null ? obj.toString() : "obj == null";  Log.i(tag, msg);  }  }   /**  * Send a {@link #WARN} log message.  *  * @param obj  */  public static void w(Object obj) {  if (Log.WARN > DEBUG_LEVEL) {  String tag = getClassName();  String msg = obj != null ? obj.toString() : "obj == null";  Log.w(tag, msg);  }  }   /**  * Send an {@link #ERROR} log message.  *  * @param obj  */  public static void e(Object obj) {  if (Log.ERROR > DEBUG_LEVEL) {  String tag = getClassName();  String msg = obj != null ? obj.toString() : "obj == null";  Log.e(tag, msg);  }  }   /**  * What a Terrible Failure: Report a condition that should never happen. The  * error will always be logged at level ASSERT with the call stack.  * Depending on system configuration, a report may be added to the  * {@link android.os.DropBoxManager} and/or the process may be terminated  * immediately with an error dialog.  *  * @param obj  */  public static void wtf(Object obj) {  if (Log.ASSERT > DEBUG_LEVEL) {  String tag = getClassName();  String msg = obj != null ? obj.toString() : "obj == null";  Log.wtf(tag, msg);  }  }   /**  * Send a {@link #VERBOSE} log message.  *  * @param tag  * Used to identify the source of a log message. It usually  * identifies the class or activity where the log call occurs.  * @param msg  * The message you would like logged.  */ public static void v(String tag, String msg) { if (Log.VERBOSE > DEBUG_LEVEL) { Log.v(tag, msg); } } /**  * Send a {@link #DEBUG_LEVEL} log message.  *  * @param tag  * Used to identify the source of a log message. It usually  * identifies the class or activity where the log call occurs.  * @param msg  * The message you would like logged.  */ public static void d(String tag, String msg) { if (Log.DEBUG > DEBUG_LEVEL) { Log.d(tag, msg); } } /**  * Send an {@link #INFO} log message.  *  * @param tag  * Used to identify the source of a log message. It usually  * identifies the class or activity where the log call occurs.  * @param msg  * The message you would like logged.  */ public static void i(String tag, String msg) { if (Log.INFO > DEBUG_LEVEL) { Log.i(tag, msg); } } /**  * Send a {@link #WARN} log message.  *  * @param tag  * Used to identify the source of a log message. It usually  * identifies the class or activity where the log call occurs.  * @param msg  * The message you would like logged.  */ public static void w(String tag, String msg) { if (Log.WARN > DEBUG_LEVEL) { Log.w(tag, msg); } } /**  * Send an {@link #ERROR} log message.  *  * @param tag  * Used to identify the source of a log message. It usually  * identifies the class or activity where the log call occurs.  * @param msg  * The message you would like logged.  */ public static void e(String tag, String msg) { if (Log.ERROR > DEBUG_LEVEL) { Log.e(tag, msg); } } /**  * What a Terrible Failure: Report a condition that should never happen. The  * error will always be logged at level ASSERT with the call stack.  * Depending on system configuration, a report may be added to the  * {@link android.os.DropBoxManager} and/or the process may be terminated  * immediately with an error dialog.  *  * @param tag  * Used to identify the source of a log message.  * @param msg  * The message you would like logged.  */ public static void wtf(String tag, String msg) { if (Log.ASSERT > DEBUG_LEVEL) { Log.wtf(tag, msg); } } /**  * Send a {@link #VERBOSE} log message. And just print method name and  * position in black.  */ public static void print() { if (Log.VERBOSE > DEBUG_LEVEL) { String tag = getClassName(); String method = callMethodAndLine(); Log.v(tag, method); if (DEBUG_SYSOUT) { System.out.println(tag + " " + method); } } } /**  * Send a {@link #DEBUG_LEVEL} log message.  *  * @param object  * The object to print.  */ public static void print(Object object) { if (Log.DEBUG > DEBUG_LEVEL) { String tag = getClassName(); String method = callMethodAndLine(); String content = ""; if (object != null) { content = object.toString() + " ---- " + method; } else { content = " ## " + " ---- " + method; } Log.d(tag, content); if (DEBUG_SYSOUT) { System.out.println(tag + " " + content + " " + method); } } } /**  * Send an {@link #ERROR} log message.  *  * @param object  * The object to print.  */ public static void printError(Object object) { if (Log.ERROR > DEBUG_LEVEL) { String tag = getClassName(); String method = callMethodAndLine(); String content = ""; if (object != null) { content = object.toString() + " ---- " + method; } else { content = " ## " + " ---- " + method; } Log.e(tag, content); if (DEBUG_SYSOUT) { System.err.println(tag + " " + method + " " + content); } } } /**  * Print the array of stack trace elements of this method in black.  *  * @return  */ public static void printCallHierarchy() { if (Log.VERBOSE > DEBUG_LEVEL) { String tag = getClassName(); String method = callMethodAndLine(); String hierarchy = getCallHierarchy(); Log.v(tag, method + hierarchy); if (DEBUG_SYSOUT) { System.out.println(tag + " " + method + hierarchy); } } } /**  * Print debug log in blue.  *  * @param object  * The object to print.  */ public static void printMyLog(Object object) { if (Log.DEBUG > DEBUG_LEVEL) { String tag = "MYLOG"; String method = callMethodAndLine(); String content = ""; if (object != null) { content = object.toString() + " ---- " + method; } else { content = " ## " + " ---- " + method; } Log.d(tag, content); if (DEBUG_SYSOUT) { System.out.println(tag + " " + content + " " + method); } } } private static String getCallHierarchy() { String result = ""; StackTraceElement[] trace = (new Exception()).getStackTrace(); for (int i = 2; i < trace.length; i++) { result += "\r\t" + trace[i].getClassName() + "." + trace[i].getMethodName() + "():" + trace[i].getLineNumber(); } return result; } private static String getClassName() { String result = ""; StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[2]; result = thisMethodStack.getClassName(); return result; } /**  * Realization of double click jump events.  *  * @return  */ private static String callMethodAndLine() { String result = "at "; StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[2]; result += thisMethodStack.getClassName() + "."; result += thisMethodStack.getMethodName(); result += "(" + thisMethodStack.getFileName(); result += ":" + thisMethodStack.getLineNumber() + ") "; return result; } } 

獲取更多請參看我Github分享的Android工具類庫 android-utils 。

 

=============================================

博客原文地址:http://ihongqiqu.com/blog/2014/10/16/android-log/

==========================================

相關文章
相關標籤/搜索