LogcatFileManager类 收集BUG Log日志

    xiaoxiao2021-03-25  23

    LogcatFileManager类是用来将Logcat信息以文件的形式保存在手机本地。

    LogcatFileManager.java文件代码如下:

    public class LogcatFileManager { private static LogcatFileManager INSTANCE = null; private static String PATH_LOGCAT; private LogDumper mLogDumper = null; private int mPId; private SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd"); private SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static LogcatFileManager getInstance() { if (INSTANCE == null) { INSTANCE = new LogcatFileManager(); } return INSTANCE; } private LogcatFileManager() { mPId = android.os.Process.myPid(); } /** * 启动LogcatManager * @param context * @param fileName 保存文件的文件夹名称 */ public void startLogcatManager(Context context,String fileName) { String folderPath = null; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { folderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + fileName;//"MMF-Logcat"; } else { folderPath = context.getFilesDir().getAbsolutePath() + File.separator + fileName;//"MMF-Logcat"; //MMF-Logcat 为文件夹名 } LogcatFileManager.getInstance().start(folderPath); } /** * 停止LogcatManager */ public void stopLogcatManager() { LogcatFileManager.getInstance().stop(); } private void setFolderPath(String folderPath) { File folder = new File(folderPath); if (!folder.exists()) { folder.mkdirs(); } if (!folder.isDirectory()) { throw new IllegalArgumentException( "The logcat folder path is not a directory: " + folderPath); } PATH_LOGCAT = folderPath.endsWith("/") ? folderPath : folderPath + "/"; // LogUtils.d(PATH_LOGCAT); } public void start(String saveDirectoy) { setFolderPath(saveDirectoy); if (mLogDumper == null) { mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT); } mLogDumper.start(); } public void stop() { if (mLogDumper != null) { mLogDumper.stopLogs(); mLogDumper = null; } } private class LogDumper extends Thread { private Process logcatProc; private BufferedReader mReader = null; private boolean mRunning = true; String cmds = null; private String mPID; private FileOutputStream out = null; public LogDumper(String pid, String dir) { mPID = pid; try { out = new FileOutputStream(new File(dir, "logcat-"+ simpleDateFormat1.format(new Date()) + ".log"), true); } catch (FileNotFoundException e) { e.printStackTrace(); } /** * * * log level:*:v , *:d , *:w , *:e , *:f , *:s * * Show the * current mPID process level of E and W log. * * */ // cmds = "logcat *:e *:w | grep \"(" + mPID + ")\""; cmds = "logcat *:e *:w | grep \"(" + mPID + ")\""; } public void stopLogs() { mRunning = false; } @Override public void run() { try { logcatProc = Runtime.getRuntime().exec(cmds); mReader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), 1024); String line = null; while (mRunning && (line = mReader.readLine()) != null) { if (!mRunning) { break; } if (line.length() == 0) { continue; } if (out != null && line.contains(mPID)) { out.write((simpleDateFormat2.format(new Date()) + " " + line + "\n").getBytes()); } } } catch (IOException e) { e.printStackTrace(); } finally { if (logcatProc != null) { logcatProc.destroy(); logcatProc = null; } if (mReader != null) { try { mReader.close(); mReader = null; } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } out = null; } } } } }

    使用方法:

    1、 将LogcatFileManager.java文件导入项目中

    2、 在Application的onCreat()和onTerminate()中添加方法,代码如下:

    public class MyBaseApplication extends Application{ @Override public void onCreate() { super.onCreate(); LogcatFileManager.getInstance().startLogcatManager(getApplicationContext(),"LJLogcat"); } @Override public void onTerminate() { super.onTerminate(); LogcatFileManager.getInstance().stopLogcatManager(); } }

    注: startLogcatManager(Context context,String fileName)中fileName是在手机上创建保存文件的文件夹名称。

    3、在Manifest.xml配置文件中添加权限:

    <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    转载请注明原文地址: https://ju.6miu.com/read-149585.html

    最新回复(0)