美文网首页程序员
日志捕获caughtExceptionHandler的封装并新增

日志捕获caughtExceptionHandler的封装并新增

作者: 爱思考的程序员 | 来源:发表于2017-06-10 13:54 被阅读0次

开发中,我们时常会遇到各种各样的崩溃问题,对于这种问题,我们的做法是try掉,让android自带的类UncaughtExceptionHandler捕获日志.但是对于这种捕获,不会显示到手机上,对于后期的维护是一大问题.于是我们就有了自定义该类的做法,捕获错误日志,让错误日志生成文件的形式保存到手机和上传到服务器.
于是我就写了一个捕获日志的工具类: CrashHandlerUtils.java
使用方式需要在Application中申明,初始化

      CrashHandlerUtils.getInstance().init(getApplicationContext());
  默认名称是 crachLog.创建默认文件夹下
   或者
     CrashHandlerUtils.getInstance().init(getApplicationContext(),"名称"); 

代码如下:
UncaughtException处理类,当程序发生Uncaught异常的时候,有该类来接管程序,并录错误报告.
需要在Application中注册,为了要在程序启动时就监控整个程序。


package com.isoftstone.randomchoose.utils;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Environment;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * UncaughtException处理类,当程序发生Uncaught异常的时候,有该类来接管程序,并记录错误报告.
 * 需要在Application中注册,为了要在程序启动时就监控整个程序。
 */
public class CrashHandlerUtils implements UncaughtExceptionHandler {
    public static final String TAG = "CrashHandler";
    //系统默认的UncaughtException处理类
    private UncaughtExceptionHandler mDefaultHandler;
    //CrashHandler实例
    private static CrashHandlerUtils instance;
    //程序的Context对象
    private Context mContext;
    //用来存储设备信息和异常信息
    private Map<String, String> infos = new HashMap<String, String>();
    //用于格式化日期,作为日志文件名的一部分
    private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //设置保存目录
    private String name = "crachLog";
    /**
     * 保证只有一个CrashHandler实例
     */
    private CrashHandlerUtils() {
    }

    /**
     * 获取CrashHandler实例 ,单例模式
     */
    public static CrashHandlerUtils getInstance() {
        if (instance == null)
            instance = new CrashHandlerUtils();
        return instance;
    }
    /**
     * 初始化
     **/
    public void init(Context context) {     
        mContext = context;
        //获取系统默认的UncaughtException处理器
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        //设置该CrashHandler为程序的默认处理器
        Thread.setDefaultUncaughtExceptionHandler(this);
    }
    /**
     * 初始化
     **/
    public void init(Context context,String dec) {
        init(context);   
        this.name = dec;
    }

    /**
     * 当UncaughtException发生时会转入该函数来处理
     **/
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        if (!handleException(ex) && mDefaultHandler != null) {
            //如果用户没有处理则让系统默认的异常处理器来处理
            mDefaultHandler.uncaughtException(thread, ex);
        } else {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                Log.e(TAG, "error : ", e);
            }
            //退出程序
            // 关闭并重启应用
            System.gc();
            _restart();
            android.os.Process.killProcess(android.os.Process.myPid());
           // android.os.Process.killProcess(android.os.Process.myPid());
           // System.exit(1);//0表示正常退出,非0表示非正常退出
        }
    }
/** 重启应用 */
    public void _restart() {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    /**
     * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
     *
     * @param ex
     * @return true:如果处理了该异常信息;否则返回false.
     */
    private boolean handleException(Throwable ex) {
        if (ex == null) {
            return false;
        }
        //收集设备参数信息
        collectDeviceInfo(mContext);
        //使用Toast来显示异常信息
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                Toast.makeText(mContext, "很抱歉,程序出现异常,即将退出.", Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }.start();
        //保存日志文件
        saveCatchInfo2File(ex);
        return true;
    }

    /**
     * 收集设备参数信息
     *
     * @param ctx
     */
    public void collectDeviceInfo(Context ctx) {
        try {
            PackageManager pm = ctx.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES);
            if (pi != null) {
                String versionName = pi.versionName == null ? "null" : pi.versionName;
                String versionCode = pi.versionCode + "";
                infos.put("versionName", versionName);
                infos.put("versionCode", versionCode);
            }
        } catch (NameNotFoundException e) {
            Log.e(TAG, "an error occured when collect package info", e);
        }
        Field[] fields = Build.class.getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                infos.put(field.getName(), field.get(null).toString());
                Log.d(TAG, field.getName() + " : " + field.get(null));
            } catch (Exception e) {
                Log.e(TAG, "an error occured when collect crash info", e);
            }
        }
    }

    /**
     * 获取存储路径
     **/
    private String getFilePath() {
        String file_dir = "";
        // SD卡是否存在
        boolean isSDCardExist = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
        // Environment.getExternalStorageDirectory()相当于File file=new File("/sdcard")
        //boolean isRootDirExist = Environment.getExternalStorageDirectory().exists();
        if (isSDCardExist) {
            file_dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+name+"/";
        } else {
            // MyApplication.getInstance().getFilesDir()返回的路劲为/data/data/PACKAGE_NAME/files,其中的包就是我们建立的主Activity所在的包
            // getRootDirectory是/system,即系统文件夹,要root了才看得到
            file_dir = Environment.getRootDirectory().getAbsolutePath() + "/"+name+"/";
        }
        return file_dir;
    }
    /**
     * 保存错误信息到文件中
     *
     * @param ex
     * @return 返回文件名称, 便于将文件传送到服务器
     */
    private String saveCatchInfo2File(Throwable ex) {
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, String> entry : infos.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            sb.append(key + "=" + value + "\n");
        }
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        ex.printStackTrace(printWriter);
        Throwable cause = ex.getCause();
        while (cause != null) {
            cause.printStackTrace(printWriter);
            cause = cause.getCause();
        }
        printWriter.close();
        String result = writer.toString();
        sb.append(result);
        try {
            String time = formatter.format(new Date());
            String fileName = "crash-" + time + ".txt";
            //获取外部存储的路径,如果直接在该路径下创建的文件夹或文件,是没有与应用关联的
            // 即使应用卸载,遗留下的文件夹或文件仍然存在
            //String file_dir = getFilePath();
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                //  getExternalFilesDir("文件夹名"),则是创建应用私有文件夹,在Android/data/包名/files/文件夹名
                File file = new File(mContext.getExternalFilesDir("crashlog"), fileName);
                Log.e(TAG, "崩溃日志:" + file.getAbsolutePath());
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                if (!file.exists()) {
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(sb.toString().getBytes());
                    fos.close();
                }
            }
            return fileName;
        } catch (Exception e) {
            Log.e(TAG, "an error occured while writing file...", e);
        }
        return null;
    }
}

常用工具类地址,在github上utils里面
戳我跳转 ------ https://github.com/Simonhy
欢迎评论和赞赏,喜欢的话收藏一下.对于上传到服务器的操作,推荐一篇
https://juejin.im/post/59342ddd0ce46300571d95b5
写的比较详细,
欢迎转载!!!
欢迎转载!!!
欢迎转载!!!

相关文章

网友评论

    本文标题:日志捕获caughtExceptionHandler的封装并新增

    本文链接:https://www.haomeiwen.com/subject/fsvefxtx.html