Android 抓取异常Log与节点Log并保存到SD卡中

好长时间没更新了,最近测试报bug不是必现的情况还很难复现,而且一不留神log就抓不到了,所以想了想还是仿照以前做手机的时候mtk有的log系统来自己写一个,参考了网上的一些例子,简简单单弄了一下,大概的功能实现了,如果有同学想要进一步丰富可以去我的GitHub上面进行修改,最好拉一个独立的分支,代码很简单,把主要的几个类贴出来,看一下就明白了。

import android.content.Context;
import android.os.Environment;

public class LogSDK {
    private static LogSDK instance;

    private LogSDK() {
    }

    public static LogSDK getInstance() {
        if (instance == null)
            instance = new LogSDK();
        return instance;
    }

    public static void init(Context context) {
        Util.applicationName = AppHelper.getApplicationName(context);
        Util.logPath = Environment.getExternalStorageDirectory() + "/" + Util.applicationName + "/";
        AppHelper.createDir(Util.logPath);
        LogToFile.init(context);
        CrashHandler.getInstance().init(context);
    }
}
import android.content.Context;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class LogToFile {
    public static final String VERBOSE = "v";
    public static final String DEBUG = "d";
    public static final String INFO = "i";
    public static final String WARN = "w";
    public static final String ERROR = "e";
    public static boolean APP_DBG = false;

    public static void init(Context context) {
        APP_DBG = AppHelper.isApkDebugable(context);
        AppHelper.createDir(Util.logPath + LogPath.LOGS);
    }

    public static String getLogType(String type) {
        return type.equals(VERBOSE) || type.equals(DEBUG) || type.equals(INFO) || type.equals(WARN) || type.equals(ERROR) ? type : "";
    }

    public static StackTraceElement getStackTraceElement() {
        StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[2];
        return stackTraceElement;
    }

    public static void logOut(Class clazz, String type, String tag, Object object) {
        if (APP_DBG) return;
        switch (getLogType(type)) {
            case "":
                return;
            default:
                writeToFile(clazz.getPackage().getName(), getStackTraceElement().getClassName() + "." + getStackTraceElement().getMethodName(), type, tag, object.toString());
                break;
        }
    }

    private static void writeToFile(String packageName, String pathName, String type, String tag, String msg) {
        if (null == Util.logPath + "Logs") {
            return;
        }
        String fileName = Util.logPath + DataHelper.getDataOfDay(LogPath.LOGS, LogPath.LOGSUFFIX);
        String log = DataHelper.getDataOfSSS() + "/" + packageName + " " + type + "/" + " " + pathName + " " + tag + " " + msg + "\n";
        FileOutputStream fos = null;
        BufferedWriter bw = null;
        try {
            fos = new FileOutputStream(fileName, true);
            bw = new BufferedWriter(new OutputStreamWriter(fos));
            bw.write(log);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
/**
 * Created by bernie.shi on 2017/3/20.
 */

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.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class CrashHandler implements UncaughtExceptionHandler {
    private Thread.UncaughtExceptionHandler mDefaultHandler;
    private static CrashHandler instance;
    private Context mContext;
    private Map<String, String> infos = new HashMap<String, String>();
    private int TIMESLEEP = 3000;

    private CrashHandler() {
    }

    public static CrashHandler getInstance() {
        if (instance == null)
            instance = new CrashHandler();
        return instance;
    }

    public void init(Context context) {
        mContext = context;
        AppHelper.createDir(getFilePath());
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        if (!handleException(ex) && mDefaultHandler != null) {
            mDefaultHandler.uncaughtException(thread, ex);
        } else {
            try {
                Thread.sleep(TIMESLEEP);
            } catch (InterruptedException e) {
            }
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(1);
        }
    }

    private boolean handleException(Throwable ex) {
        if (ex == null) {
            return false;
        }
        collectDeviceInfo(mContext);
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                Looper.loop();
            }
        }.start();
        saveCatchInfo2File(ex);
        return true;
    }

    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) {
        }
        Field[] fields = Build.class.getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                infos.put(field.getName(), field.get(null).toString());
            } catch (Exception e) {
            }
        }
    }

    private String getFilePath() {
        return Environment.getExternalStorageDirectory() + "/" + Util.applicationName + LogPath.LOGCRASH;
    }

    private void 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 fileName = DataHelper.getDataOfDay(LogPath.CRASH, LogPath.LOGSUFFIX);
            File file = AppHelper.createFile(getFilePath() + fileName);
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(sb.toString().getBytes());
            fos.close();
        } catch (Exception e) {
        }
    }

}

我的github项目地址:https://github.com/ss831116/LogLibrary.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值