Android輸出日誌Log類並儲存到檔案中

2021-10-01 13:59:52 字數 2115 閱讀 3619

android.util.log常用的方法有以下5個: 

log.v() log.d() log.i() log.w() 以及 log.e()。根據首字母分別對應verbose,debug,info,warn,error。 

1、log.v 的除錯顏色為黑色的,任何訊息都會輸出,這裡的v代表verbose囉嗦的意思,平時使用就是log.v("",""); 

2、log.d的輸出顏色是藍色的,僅輸出debug除錯的意思,但他會輸出上層的資訊,過濾起來可以通過ddms的logcat標籤來選擇。

3、log.i的輸出為綠色,一般提示性的訊息information,它不會輸出log.v和log.d的資訊,但會顯示i、w和e的資訊。

4、log.w的意思為橙色,可以看作為warning警告,一般需要我們注意優化android**,同時選擇它後還會輸出log.e的資訊。 

5、log.e為紅色,可以想到error錯誤,這裡僅顯示紅色的錯誤資訊,這些錯誤就需要我們認真的分析,檢視棧的資訊了。

public class mylog 

public static void e(string tag, object msg)

public static void d(string tag, object msg)

public static void i(string tag, object msg)

public static void v(string tag, object msg)

public static void w(string tag, string text)

public static void e(string tag, string text)

public static void d(string tag, string text)

public static void i(string tag, string text)

public static void v(string tag, string text)

/*** 根據tag, msg和等級,輸出日誌

* @param tag

* @param msg

* @param level

*/private static void log(string tag, string msg, char level) else if ('w' == level && ('w' == mylog_type || 'v' == mylog_type)) else if ('d' == level && ('d' == mylog_type || 'v' == mylog_type)) else if ('i' == level && ('d' == mylog_type || 'v' == mylog_type)) else

if (mylog_write_to_file)//日誌寫入檔案開關

writelogtofile(string.valueof(level), tag, msg);}}

/*** 開啟日誌檔案並寫入日誌

* @param mylogtype

* @param tag

* @param text

*/private static void writelogtofile(string mylogtype, string tag, string text)

"建立檔案","建立檔案");

file file = new file(dirsfile.tostring(), needwritefiel + mylogfilename);// mylog_path_sdcard_dir

if (!file.exists()) catch (exception e)

}try catch (ioexception e)

}/**

* 刪除制定的日誌檔案

*/public static void delfile()

}/**

* 得到現在時間前的幾天日期,用來得到需要刪除的日誌檔名

*/private static date getdatebefore()

}

注:還需要在androidmanifest.xml檔案中新增許可權

Android輸出日誌Log類

android.util.log常用的方法有以下5個 log.v log.d log.i log.w 以及 log.e 根據首字母分別對應verbose,debug,info,warn,error。1 log.v 的除錯顏色為黑色的,任何訊息都會輸出,這裡的v代表verbose囉嗦的意思,平時使用就...

Android輸出日誌Log類並儲存到檔案中

android.util.log常用的方法有以下5個 log.v log.d log.i log.w 以及 log.e 根據首字母分別對應verbose,debug,info,warn,error。1 log.v 的除錯顏色為黑色的,任何訊息都會輸出,這裡的v代表verbose囉嗦的意思,平時使用就...

Android手機抓取log日誌

adb.exe adbwinapi.dll adbwinusbapi.dll 這三個檔案即可使用 在pc端解壓縮,得到platform tools latest windows資料夾 此處將此資料夾放在d盤根目錄下 2 手機在開發者模式下,開啟usb除錯功能 看自己具體的手機操作 然後用usb線連線...