现在的位置: 首页 > 综合 > 正文

[代码片段] 【Android】 Log类

2014年12月12日 ⁄ 综合 ⁄ 共 3581字 ⁄ 字号 评论关闭

Android开发一些时间的人都知道,打log对于开发来说是多么的重要,但是如果项目大了,如果你用原生的log你会发现不好管理,而且建议应用发布之前最好把log都屏蔽掉,如果一个个注释那肯定要疯掉了,到时候还得去注释。我整合了一个类,可以把可以随时关闭和打开log,同时还可以将log写入sd相应的文件。

LogUtils.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.os.Environment;
import android.util.Log;

/**
*  日志信息
* @author Harlan Song
* @createDate 2013 -2- 8
*/
public class LogUtils {

      
       /**
       * 日志开关
       */
       protected static final boolean LOG_OPEN_DEBUG =true ;
       protected static final boolean LOG_OPEN_POINT =true ;
      
       /**
       * 日志类型开关,必须 LOG_OPEN_DEBUG = true的时候才能启作用
       */
       protected static boolean logOpeni = true;
       protected static boolean logOpend = true;
       protected static boolean logOpenw = true;
       protected static boolean logOpene = true;
      
       /**
       * 日志目录
       */
    protected static final String PATH_ROOT = Environment.getExternalStorageDirectory() + "/projectName/";
    protected static final String PATH_LOG_INFO = PATH_ROOT+"info/" ;
    protected static final String PATH_LOG_WARNING = PATH_ROOT+"warning/" ;
    protected static final String PATH_LOG_ERROR = PATH_ROOT+"error/" ;

       public static void d(String tag,String message) {
             if( message!=null && message!=null){
                   if(LOG_OPEN_DEBUG && logOpend){
                        Log. d(tag, message);
                  }
                   if(LOG_OPEN_POINT )
                         point(PATH_LOG_INFO,tag,message);
            }
            
      }
      
       public static void i(String tag,String message) {
             if( message!=null && message!=null){
                   if(LOG_OPEN_DEBUG && logOpeni){
                        Log. i(tag, message);
                  }
                   if(LOG_OPEN_POINT )
                         point(PATH_LOG_INFO,tag,message);
            }
            
      }
      
       public static void w(String tag,String message) {
             if( message!=null && message!=null){
                   if(LOG_OPEN_DEBUG && logOpenw){
                        Log. w(tag, message);
                  }
                   if(LOG_OPEN_POINT )
                         point(PATH_LOG_WARNING,tag,message);
            }
            
      }

       public static void e(String tag,String message) {
             if( message!=null && message!=null){
                   if(LOG_OPEN_DEBUG && logOpene){
                        Log. e(tag, message);
                  }
                   if(LOG_OPEN_POINT )
                         point(PATH_LOG_ERROR,tag,message);
            }
            
      }
      
       public static void point(String path, String tag,String msg) {
         Date date = new Date();
         SimpleDateFormat dateFormat = new SimpleDateFormat("" ,Locale.SIMPLIFIED_CHINESE );
         dateFormat.applyPattern( "yyyy");
         path = path + dateFormat.format(date) + "/";
         dateFormat.applyPattern( "MM");
         path +=dateFormat.format(date)+ "/";
         dateFormat.applyPattern( "dd");
         path +=dateFormat.format(date)+ ".log";
         dateFormat.applyPattern( "[yyyy-MM-dd HH:mm:ss]");
         String time=dateFormat.format(date);
         File file = new File(path);
          if (!file.exists())
                createDipPath(path);   
         BufferedWriter out = null;   
     try {   
         out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true )));   
         out.write(time+ " " + tag + " " +msg +"\r\n" );   
     } catch (Exception e) {   
         e.printStackTrace();   
     } finally {   
         try {   
             if(out != null){
                 out.close();   
             }
         } catch (IOException e) {   
             e.printStackTrace();   
         }   
     } 
   };
  
    /**
    * 根据文件路径 递归创建文件
    *
    * @param file
    */
    public static void createDipPath(String file) {
         String parentFile = file.substring(0, file.lastIndexOf( "/" ));
         File file1 = new File(file);
         File parent = new File(parentFile);
          if (!file1.exists()) {
                 parent.mkdirs();
                  try {
                       file1.createNewFile();
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
         }
   }
}
复制代码
PS:代码原理很简单,但有的时候却能帮你做很多事情,祝大家coding愉快。

抱歉!评论已关闭.