Android日志工具类

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
import org.apache.commons.lang3.time.DateFormatUtils;  
  
import android.os.Environment;  
import android.util.Log;  
  
/** 
 * 日志记录 
 *  
 */  
public class LogUtil {  
    /** 
     * 开发阶段 
     */  
    private static final int DEVELOP = 0;  
    /** 
     * 内部测试阶段 
     */  
    private static final int DEBUG = 1;  
    /** 
     * 公开测试 
     */  
    private static final int BATE = 2;  
    /** 
     * 正式版 
     */  
    private static final int RELEASE = 3;  
  
    /** 
     * 当前阶段标示 
     */  
    private static int currentStage = DEVELOP;  
  
    private static String path;  
    private static File file;  
    private static FileOutputStream outputStream;  
    private static String pattern = "yyyy-MM-dd HH:mm:ss";  
  
    static {  
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {  
            File externalStorageDirectory = Environment.getExternalStorageDirectory();  
            path = externalStorageDirectory.getAbsolutePath() + "/zcw/";  
            File directory = new File(path);  
            if (!directory.exists()) {  
                directory.mkdirs();  
            }  
            file = new File(new File(path), "log.txt");  
            android.util.Log.i("SDCAEDTAG", path);  
            try {  
                outputStream = new FileOutputStream(file, true);  
            } catch (FileNotFoundException e) {  
  
            }  
        } else {  
  
        }  
    }  
  
    public static void info(String msg) {  
        info(LogUtil.class, msg);  
    }  
  
    public static void info(Class clazz, String msg) {  
        switch (currentStage) {  
            case DEVELOP:  
                // 控制台输出  
                Log.i(clazz.getSimpleName(), msg);  
                break;  
            case DEBUG:  
                // 在应用下面创建目录存放日志  
  
                break;  
            case BATE:  
                // 写日志到sdcard  
                Date date = new Date();  
                String time = DateFormatUtils.format(date, pattern);  
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {  
                    if (outputStream != null) {  
                        try {  
                            outputStream.write(time.getBytes());  
                            String className = "";  
                            if (clazz != null) {  
                                className = clazz.getSimpleName();  
                            }  
                            outputStream.write(("    " + className + "\r\n").getBytes());  
  
                            outputStream.write(msg.getBytes());  
                            outputStream.write("\r\n".getBytes());  
                            outputStream.flush();  
                        } catch (IOException e) {  
  
                        }  
                    } else {  
                        android.util.Log.i("SDCAEDTAG", "file is null");  
                    }  
                }  
                break;  
            case RELEASE:  
                // 一般不做日志记录  
                break;  
        }  
    }  
}  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:iOS中url的特殊字符转换

下一篇:Android静默安装和静默卸载代码