android:Log日志保存到本地
2015-11-20 15:06
555 查看
大家在开发一个应用进行调试的时候,查看log日志信息是必不可少的,因此log信息的保存是非常有必要的。下面是log日志保存的一个工具类,仅供参考。
package com.dandy.util; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.text.SimpleDateFormat; import android.annotation.SuppressLint; import android.content.Context; import android.os.Environment; import android.text.TextUtils; /** * * @author Administrator * * log打印日志保存,文件的保存以小时为单位 * permission:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> * <uses-permission android:name="android.permission.READ_LOGS" /> */ public class LogCatHelper { private static LogCatHelper instance = null; private String dirPath;//保存路径 private int appid;//应用pid private Thread logThread; /** * @param mContext * @param path log日志保存根目录 * @return */ public static LogCatHelper getInstance(Context mContext,String path){ if(instance == null){ instance = new LogCatHelper(mContext,path); } return instance; } private LogCatHelper(Context mContext,String path) { appid = android.os.Process.myPid(); if(TextUtils.isEmpty(path)){ dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator+"seeker"+File.separator+mContext.getPackageName(); }else{ dirPath = path; } File dir = new File(dirPath); if(!dir.exists()){ dir.mkdirs(); } } /** * 启动log日志保存 */ public void start(){ if(logThread == null){ logThread = new Thread(new LogRunnable(appid, dirPath)); } logThread.start(); } private static class LogRunnable implements Runnable{ private Process mProcess; private FileOutputStream fos; private BufferedReader mReader; private String cmds; private String mPid; public LogRunnable(int pid,String dirPath) { this.mPid = ""+pid; try { File file = new File(dirPath,FormatDate.getFormatDate()+".log"); if(!file.exists()){ file.createNewFile(); } fos = new FileOutputStream(file,true); } catch (Exception e) { e.printStackTrace(); } cmds = "logcat *:v | grep \"(" + mPid + ")\""; } @Override public void run() { try { mProcess = Runtime.getRuntime().exec(cmds); mReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()),1024); String line; while((line = mReader.readLine()) != null){ if(line.length() == 0){ continue; } if(fos != null && line.contains(mPid)){ fos.write((FormatDate.getFormatTime()+" "+line+"\r\n").getBytes()); } } } catch (Exception e) { e.printStackTrace(); }finally{ if(mProcess != null){ mProcess.destroy(); mProcess = null; } try { if(mReader != null){ mReader.close(); mReader = null; } if(fos != null){ fos.close(); fos = null; } } catch (Exception e2) { e2.printStackTrace(); } } } } @SuppressLint("SimpleDateFormat") private static class FormatDate{ public static String getFormatDate(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH"); return sdf.format(System.currentTimeMillis()); } public static String getFormatTime(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(System.currentTimeMillis()); } } }
相关文章推荐
- 服务器基于PHP CodeIgniter,Android基于Volley实现多文件/图片上传(含服务器,web版和android客户端完整代码)
- Android编程实现等比例显示图片的方法
- Android 触摸手势基础 官方文档概览
- android 多线程数据库读写分析与优化【转】
- Android屏幕适配经验谈
- Android 4K高清图片解码显示(2)BitmapFactory常用Bitmap的解码方法
- Android内存管理
- Android高仿qq登陆界面
- 【FastDev4Android框架开发】RecyclerView完全解析之打造新版类Gallery效果(二十九)
- ViewPager进阶,轻松实现简拼的splash界面
- android安装apk错误Failure [INSTALL_FAILED_INVALID_URI]的解决办法
- Android程序国际化
- Android触控基础 MotionEvent
- Android fragment 中控制某一个界面全屏与否
- vlc-android在Ubuntu下编译记录
- Android里Service的bindService()和startService()混合使用深入分析
- Android编程之截屏实现方法(包括scrollview与listview)
- 更改 android realtek的系统权限
- android中RelativeLayout无法填充ScrollView布局的问题
- android 2D绘图