android上一個(gè)可追蹤代碼具體到函數(shù)某行的日志類
更新時(shí)間:2012年12月25日 10:51:08 作者:
追蹤代碼到函數(shù)具體某行,這樣的功能,是每一個(gè)程序員都希望會(huì)有的,因?yàn)樗梢詭椭覀冏粉櫟侥承写a的錯(cuò)誤,接下來介紹下android上一個(gè)可追蹤代碼到函數(shù)具體某行的日志類,希望對開發(fā)者有所幫助
代碼如下:
package xiaogang.enif.utils;
/**
* The Class LogUtils for log printing, which help us
* easy to trace our codes or logics in the project .
*
* @author zhao xiaogang
* @time 2011.4.12
*/
public class LogUtils {
private final static int VERBOSE = 0;
private final static int DEBUG = 1;
private final static int INFO = 2;
private final static int WARN = 3;
private final static int ERROR = 4;
private final static int DEFAULT_LEVEL = -1;
private int level;
private final String clazz;
private static final String TAG = "LogUtils";
public static LogUtils getDebugLog(Class<?> clazz, int l) {
LogUtils log = new LogUtils(clazz);
log.level = l;
return log;
}
public static LogUtils getLog(Class<?> clazz) {
return new LogUtils(clazz);
}
public LogUtils(Class<?> clazz) {
this.clazz = "[" + clazz.getSimpleName() + "] ";
level = DEFAULT_LEVEL;
}
public void verbose(String message) {
verbose(message, null);
}
public void debug(String message) {
debug(message, null);
}
public void info(String message) {
info(message, null);
}
public void warn(String message) {
warn(message, null);
}
public void error(String message) {
error(message, null);
}
public void verbose(String message, Throwable t) {
if (VERBOSE < level)
return;
if (message != null)
android.util.Log.v(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.v(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}
public void debug(String message, Throwable t) {
if (DEBUG < level)
return;
if (message != null)
android.util.Log.d(clazz, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.d(clazz, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}
public void info(String message, Throwable t) {
if (INFO < level)
return;
if (message != null)
android.util.Log.i(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.i(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}
public void warn(String message, Throwable t) {
if (WARN < level)
return;
if (message != null)
android.util.Log.w(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.w(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}
public void error(String message, Throwable t) {
if (ERROR < level)
return;
if (message != null)
android.util.Log.e(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.e(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}
private static int getLineNumber() {
return Thread.currentThread().getStackTrace()[5].getLineNumber();
}
}
好用的話,記得給好評(píng),嘿嘿!
復(fù)制代碼 代碼如下:
package xiaogang.enif.utils;
/**
* The Class LogUtils for log printing, which help us
* easy to trace our codes or logics in the project .
*
* @author zhao xiaogang
* @time 2011.4.12
*/
public class LogUtils {
private final static int VERBOSE = 0;
private final static int DEBUG = 1;
private final static int INFO = 2;
private final static int WARN = 3;
private final static int ERROR = 4;
private final static int DEFAULT_LEVEL = -1;
private int level;
private final String clazz;
private static final String TAG = "LogUtils";
public static LogUtils getDebugLog(Class<?> clazz, int l) {
LogUtils log = new LogUtils(clazz);
log.level = l;
return log;
}
public static LogUtils getLog(Class<?> clazz) {
return new LogUtils(clazz);
}
public LogUtils(Class<?> clazz) {
this.clazz = "[" + clazz.getSimpleName() + "] ";
level = DEFAULT_LEVEL;
}
public void verbose(String message) {
verbose(message, null);
}
public void debug(String message) {
debug(message, null);
}
public void info(String message) {
info(message, null);
}
public void warn(String message) {
warn(message, null);
}
public void error(String message) {
error(message, null);
}
public void verbose(String message, Throwable t) {
if (VERBOSE < level)
return;
if (message != null)
android.util.Log.v(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.v(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}
public void debug(String message, Throwable t) {
if (DEBUG < level)
return;
if (message != null)
android.util.Log.d(clazz, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.d(clazz, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}
public void info(String message, Throwable t) {
if (INFO < level)
return;
if (message != null)
android.util.Log.i(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.i(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}
public void warn(String message, Throwable t) {
if (WARN < level)
return;
if (message != null)
android.util.Log.w(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.w(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}
public void error(String message, Throwable t) {
if (ERROR < level)
return;
if (message != null)
android.util.Log.e(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
if (t != null)
android.util.Log.e(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
}
private static int getLineNumber() {
return Thread.currentThread().getStackTrace()[5].getLineNumber();
}
}
好用的話,記得給好評(píng),嘿嘿!
您可能感興趣的文章:
- Android adb logcat 命令查看日志詳細(xì)介紹
- microlog4android將Android Log日志寫到SD卡文件中實(shí)現(xiàn)方法
- Android 日志系統(tǒng)Logger源代碼詳細(xì)介紹
- Android開發(fā)之在程序中時(shí)時(shí)獲取logcat日志信息的方法(附demo源碼下載)
- Android SD卡上文件操作及記錄日志操作實(shí)例分析
- Python實(shí)現(xiàn)過濾單個(gè)Android程序日志腳本分享
- logcat命令使用方法和查看android系統(tǒng)日志緩沖區(qū)內(nèi)容的方法
- android輕松管理安卓應(yīng)用中的log日志 發(fā)布應(yīng)用時(shí)log日志全部去掉的方法
- android 捕獲系統(tǒng)異常并上傳日志具體實(shí)現(xiàn)
- Mac 下 Android Studio 不打印日志的解決辦法
相關(guān)文章
android短信管理器SmsManager實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了android短信管理器SmsManager實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android Activity之間相互調(diào)用與傳遞參數(shù)的原理與用法分析
這篇文章主要介紹了Android Activity之間相互調(diào)用與傳遞參數(shù)的原理與用法,較為詳細(xì)的分析了Android組件的構(gòu)成以及Activity的創(chuàng)建、調(diào)用、切換等相關(guān)操作技巧,需要的朋友可以參考下2016-08-08Android手機(jī)衛(wèi)士之確認(rèn)密碼對話框
這篇文章主要為大家詳細(xì)介紹了Android手機(jī)衛(wèi)士之確認(rèn)密碼對話框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android使用Scrolling機(jī)制實(shí)現(xiàn)Tab吸頂效果
app 首頁中經(jīng)常要實(shí)現(xiàn)首頁頭卡共享,tab 吸頂,內(nèi)容區(qū)通過 ViewPager 切換的需求,以前往往是利用事件處理來完成,但是這些也有一定的弊端和滑動(dòng)方面不如意的地方,本文我們利用NestedScrolling機(jī)制來實(shí)現(xiàn),感興趣的朋友可以參考下2024-01-01Android實(shí)現(xiàn)自定義手勢和識(shí)別手勢的功能
這篇文章主要介紹了Android實(shí)現(xiàn)自定義手勢和識(shí)別手勢的功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10Android Gradle Build Error:Some file crunching failed, see l
這篇文章主要介紹了Android Gradle Build Error:Some file crunching failed, see logs for details的快速解決方法的相關(guān)資料,需要的朋友可以參考下2016-10-10Android開發(fā)之EditText框輸入清理工具類示例
這篇文章主要介紹了Android開發(fā)之EditText框輸入清理工具類,涉及Android事件監(jiān)聽及輸入框清理相關(guān)操作技巧,需要的朋友可以參考下2018-01-01