app如何進行崩潰日志收集

單例模式的崩潰日志管理類,目前可以保存到本地txt文檔,考慮加入
日志上傳。

 private static final String LOG_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + "/crash/log/";
    private static  String LOG_NAME ;
    private ArrayList<Activity> list = new ArrayList<Activity>();

    private  MyCatchExceptionHandler handler = null;
    private  static   CrashManage  ins;
    private CrashManage(){};

    public static CrashManage getIns(){
        if(ins == null){
            synchronized (CrashManage.class){
                if(ins == null)
                    ins = new CrashManage();
            }
        }
        return ins;
    }

    public  void init(String appName){
        LOG_NAME = appName +"_"+getCurrentDateString() + ".txt";

        if(handler  ==  null){
            handler = new MyCatchExceptionHandler();
        }
        Thread.setDefaultUncaughtExceptionHandler(handler);
    }



    private class MyCatchExceptionHandler implements Thread.UncaughtExceptionHandler{

        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            writeErrorLog(ex);
            exit();
        }
    }
    /**
     * 打印錯誤日志
     *
     * @param ex
     */
    protected void writeErrorLog(Throwable ex) {
        String info = null;
        ByteArrayOutputStream baos = null;
        PrintStream printStream = null;
        try {
            baos = new ByteArrayOutputStream();
            printStream = new PrintStream(baos);
            ex.printStackTrace(printStream);
            byte[] data = baos.toByteArray();
            info = new String(data);
            data = null;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (printStream != null) {
                    printStream.close();
                }
                if (baos != null) {
                    baos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Log.d("crashlog", "崩潰信息\n" + info);
        File dir = new File(LOG_DIR);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir, LOG_NAME);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file, true);
            fileOutputStream.write(info.getBytes());
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 獲取當前日期
     *
     * @return
     */
    private static String getCurrentDateString() {
        String result = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",
                Locale.getDefault());
        Date nowDate = new Date();
        result = sdf.format(nowDate);
        return result;
    }

    /**
     * Activity關閉時,刪除Activity列表中的Activity對象
     */
    public void removeActivity(Activity a) {
        list.remove(a);
    }

    /**
     * 向Activity列表中添加Activity對象
     */
    public void addActivity(Activity a) {
        list.add(a);
    }

    /**
     * 關閉Activity列表中的所有Activity
     */
    public void exit() {
        for (Activity activity : list) {
            if (null != activity) {
                activity.finish();
            }
        }
        // 殺死該應用進程
        android.os.Process.killProcess(android.os.Process.myPid());
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,422評論 25 708
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399