竹山一叶

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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     }
103104     /**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     }
115116     /**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" />






posted on 2017-05-08 23:49  竹山一叶  阅读(10572)  评论(0编辑  收藏  举报