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啰嗦的意思,平時使用就是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_DIR
111         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" />


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM