android上一個(gè)可追蹤代碼具體到函數(shù)某行的日志類
更新時(shí)間:2012年12月25日 10:51:08 作者:
追蹤代碼到函數(shù)具體某行,這樣的功能,是每一個(gè)程序員都希望會有的,因?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();
}
}
好用的話,記得給好評,嘿嘿!
復(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();
}
}
好用的話,記得給好評,嘿嘿!
您可能感興趣的文章:
- 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使用Photoview實(shí)現(xiàn)圖片左右滑動及縮放功能
這篇文章主要為大家詳細(xì)介紹了Android使用Photoview實(shí)現(xiàn)圖片左右滑動及縮放功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
封裝的android監(jiān)聽手指左右滑動屏幕的事件類分享
這篇文章主要介紹了封裝的android監(jiān)聽手指左右滑動屏幕的事件類分享,本文分別給出了簡單處理方法的代碼和封裝好的處理類代碼,需要的朋友可以參考下2015-05-05
Android 修改Preferences默認(rèn)樣式的步驟
這篇文章主要介紹了Android 修改Preferences默認(rèn)樣式的步驟,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-04-04
關(guān)于Android中點(diǎn)擊通知欄的通知啟動Activity問題解決
這篇文章主要介紹了關(guān)于解決Android中點(diǎn)擊通知欄的通知啟動Activity問題的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03
Android 中 android.view.WindowLeaked的解決辦法
這篇文章主要介紹了Android 中 android.view.WindowLeaked的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05
android內(nèi)存優(yōu)化之圖片優(yōu)化
對圖片本身進(jìn)行操作。盡量不要使用setImageBitmap、setImageResource、BitmapFactory.decodeResource來設(shè)置一張大圖,因?yàn)檫@些方法在完成decode后,最終都是通過java層的createBitmap來完成的,需要消耗更多內(nèi)存2012-12-12

