Android輸出日誌Log類

來源:互聯網
上載者:User

標籤:android   style   blog   io   color   os   ar   使用   for   

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錯誤,這裡僅顯示紅色的錯誤資訊,這些錯誤就需要我們認真的分析,查看棧的資訊了。

 

  1 public class MyLog {  2     private static Boolean MYLOG_SWITCH = true; // 記錄檔總開關  3     private static Boolean MYLOG_WRITE_TO_FILE = true;// 日誌寫入檔案開關  4     private static char MYLOG_TYPE = ‘v‘;// 輸入日誌類型,w代表只輸出警示資訊等,v代表輸出所有資訊  5     private static String MYLOG_PATH_SDCARD_DIR = "/sdcard/";// 記錄檔在sdcard中的路徑  6     private static int SDCARD_LOG_FILE_SAVE_DAYS = 0;// sd卡中記錄檔的最多儲存天數  7     private static String MYLOGFILEName = "Log.txt";// 本類輸出的記錄檔名稱  8     private static SimpleDateFormat myLogSdf = new SimpleDateFormat(  9             "yyyy-MM-dd HH:mm:ss");// 日誌的輸出格式 10     private static SimpleDateFormat logfile = new SimpleDateFormat("yyyy-MM-dd");// 記錄檔格式 11     public Context context; 12  13     public static void w(String tag, Object msg) { // 警告資訊 14         log(tag, msg.toString(), ‘w‘); 15     } 16  17     public static void e(String tag, Object msg) { // 錯誤資訊 18         log(tag, msg.toString(), ‘e‘); 19     } 20  21     public static void d(String tag, Object msg) {// 調試資訊 22         log(tag, msg.toString(), ‘d‘); 23     } 24  25     public static void i(String tag, Object msg) {// 26         log(tag, msg.toString(), ‘i‘); 27     } 28  29     public static void v(String tag, Object msg) { 30         log(tag, msg.toString(), ‘v‘); 31     } 32  33     public static void w(String tag, String text) { 34         log(tag, text, ‘w‘); 35     } 36  37     public static void e(String tag, String text) { 38         log(tag, text, ‘e‘); 39     } 40  41     public static void d(String tag, String text) { 42         log(tag, text, ‘d‘); 43     } 44  45     public static void i(String tag, String text) { 46         log(tag, text, ‘i‘); 47     } 48  49     public static void v(String tag, String text) { 50         log(tag, text, ‘v‘); 51     } 52  53     /** 54      * 根據tag, msg和等級,輸出日誌 55      *  56      * @param tag 57      * @param msg 58      * @param level 59      * @return void 60      * @since v 1.0 61      */ 62     private static void log(String tag, String msg, char level) { 63         if (MYLOG_SWITCH) { 64             if (‘e‘ == level && (‘e‘ == MYLOG_TYPE || ‘v‘ == MYLOG_TYPE)) { // 輸出錯誤資訊 65                 Log.e(tag, msg); 66             } else if (‘w‘ == level && (‘w‘ == MYLOG_TYPE || ‘v‘ == MYLOG_TYPE)) { 67                 Log.w(tag, msg); 68             } else if (‘d‘ == level && (‘d‘ == MYLOG_TYPE || ‘v‘ == MYLOG_TYPE)) { 69                 Log.d(tag, msg); 70             } else if (‘i‘ == level && (‘d‘ == MYLOG_TYPE || ‘v‘ == MYLOG_TYPE)) { 71                 Log.i(tag, msg); 72             } else { 73                 Log.v(tag, msg); 74             } 75             if (MYLOG_WRITE_TO_FILE) 76                 writeLogtoFile(String.valueOf(level), tag, msg); 77         } 78     } 79  80     /** 81      * 開啟記錄檔並寫入日誌 82      *  83      * @return 84      * **/ 85     private static void writeLogtoFile(String mylogtype, String tag, String text) {// 建立或開啟記錄檔 86         Date nowtime = new Date(); 87         String needWriteFiel = logfile.format(nowtime); 88         String needWriteMessage = myLogSdf.format(nowtime) + "    " + mylogtype 89                 + "    " + tag + "    " + text; 90         File dirPath = Environment.getExternalStorageDirectory(); 91         File file = new File(dirPath.toString(), needWriteFiel + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR 92         try { 93             FileWriter filerWriter = new FileWriter(file, true);// 後面這個參數代表是不是要接上檔案中原來的資料,不進行覆蓋 94             BufferedWriter bufWriter = new BufferedWriter(filerWriter); 95             bufWriter.write(needWriteMessage); 96             bufWriter.newLine(); 97             bufWriter.close(); 98             filerWriter.close(); 99         } catch (IOException e) {100             e.printStackTrace();101         }102     }103 104     /**105      * 刪除制定的記錄檔106      * */107     public static void delFile() {// 刪除記錄檔108         String needDelFiel = logfile.format(getDateBefore());109         File dirPath = Environment.getExternalStorageDirectory();110         File file = new File(dirPath, needDelFiel + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR111         if (file.exists()) {112             file.delete();113         }114     }115 116     /**117      * 得到現在時間前的幾天日期,用來得到需要刪除的記錄檔名118      * */119     private static Date getDateBefore() {120         Date nowtime = new Date();121         Calendar now = Calendar.getInstance();122         now.setTime(nowtime);123         now.set(Calendar.DATE, now.get(Calendar.DATE)124                 - SDCARD_LOG_FILE_SAVE_DAYS);125         return now.getTime();126     }127 }


註:還需要在AndroidManifest.xml檔案中添加許可權
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Android輸出日誌Log類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.