Android 崩溃日志的获取方式

1.方式一:

Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        writeErrorLog(e);
    }
};

protected void writeErrorLog(Throwable throwable) {
    String info = null;
    ByteArrayOutputStream outputStream = null;
    PrintStream printStream = null;
    try {
        outputStream = new ByteArrayOutputStream();
        printStream = new PrintStream(outputStream);
        throwable.printStackTrace(printStream);
        byte[] data = outputStream.toByteArray();
        info = new String(data);
        data = null;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (printStream != null) {
                printStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    File dir = new File(Error_Log);
    if (dir.exists()) {
        dir.mkdir();
    }
    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();
    }
}

2.方式二:

依赖它 

compile 'com.github.wenmingvs:LogReport:1.0.3'
MyApplication中:
LogReport.getInstance()
        .setCacheSize(30 * 1024 * 1024)//支持设置缓存大小,超出后清空
        .setLogDir(getApplicationContext(), Environment.getExternalStorageDirectory().
                getAbsolutePath() + "/" + this.getString(this.getApplicationInfo().labelRes) + "/")//定义路径为:sdcard/[app name]/
        .setWifiOnly(true)//设置只在Wifi状态下上传,设置为false为Wifi和移动网络都上传
        .setLogSaver(new CrashWriter(getApplicationContext()))//支持自定义保存崩溃信息的样式
        //.setEncryption(new AESEncode()) //支持日志到AES加密或者DES加密,默认不开启
        .init(getApplicationContext());

你可能感兴趣的:(崩溃日志,Android)