前提
今天在群里聊天的時候有群友問如何捕獲錯誤日志,我說可以自己寫,也可以用第三方的比如騰訊的bugly,友盟的錯誤統計等等,但是那些是別人的東西,作為一個程序員當然是要知其然,并且要知其所以然。因此今天就在此寫一下關于捕獲錯誤日志的文章,希望可以給新手指導,大佬請繞行。
首先
要捕獲錯誤日志當然是調用系統的了,這樣最方便,也是大家常用的了,廢話不多說,直接上圖,no pic say a xx.
錯誤日志.png
其次
上面的圖是日志信息,下面來看看代碼如何編寫。
捕獲錯誤日志信息類
public class CrashHandler implements UncaughtExceptionHandler { private static final String TAG = "CrashHandler"; private static final boolean DEBUG = true; private static final String FILE_NAME = "crash";// log文件的后綴名private static final String FILE_NAME_SUFFIX = ".txt";private static CrashHandler sInstance = new CrashHandler();// 系統默認的異常處理(默認情況下,系統會終止當前的異常程序)private UncaughtExceptionHandler mDefaultCrashHandler;private Context mContext;//log路徑private String mLogPath=null;// 構造方法私有,防止外部構造多個實例,即采用單例模式private CrashHandler() {}public static CrashHandler getInstance() { return sInstance;}// 這里主要完成初始化工作public void init(Context context,String logPath) { // 獲取系統默認的異常處理器 mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler(); // 將當前實例設為系統默認的異常處理器 Thread.setDefaultUncaughtExceptionHandler(this); // 獲取Context,方便內部使用 mContext = context.getApplicationContext(); this.mLogPath=logPath;}/** * 這個是最關鍵的函數,當程序中有未被捕獲的異常,系統將會自動調用#uncaughtException方法 * thread為出現未捕獲異常的線程,ex為未捕獲的異常,有了這個ex,我們就可以得到異常信息。 */@Overridepublic void uncaughtException(Thread thread, Throwable ex) { try { // 導出異常信息到SD卡中 dumpExceptionToSDCard(ex); // 這里可以通過網絡上傳異常信息到服務器,便于開發人員分析日志從而解決bug uploadExceptionToServer(); } catch (IOException e) { e.printStackTrace(); } // 打印出當前調用棧信息 ex.printStackTrace(); // 如果系統提供了默認的異常處理器,則交給系統去結束我們的程序,否則就由我們自己結束自己 if (mDefaultCrashHandler != null) { mDefaultCrashHandler.uncaughtException(thread, ex); } else { Process.killProcess(Process.myPid()); }}/** * 寫入SD卡 * * @param ex * @throws IOException */@SuppressLint("SimpleDateFormat")private void dumpExceptionToSDCard(Throwable ex) throws IOException { // 如果SD卡不存在或無法使用,則無法把異常信息寫入SD卡 if (!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { if (DEBUG) { Log.e(TAG, "sdcard unmounted,skip dump exception"); return; } } File dir = new File(mLogPath); if (!dir.exists()) { dir.mkdirs(); } long current = System.currentTimeMillis(); String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") .format(new Date(current)); // 以當前時間創建log文件 File file = new File(mLogPath + FILE_NAME + time + FILE_NAME_SUFFIX); try { PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter( file))); // 導出發生異常的時間 pw.println(time); // 導出手機信息 dumpPhoneInfo(pw); pw.println(); // 導出異常的調用棧信息 ex.printStackTrace(pw); pw.close(); } catch (Exception e) { Log.e(TAG, "dump crash info failed"); }}private void dumpPhoneInfo(PrintWriter pw) throws NameNotFoundException { // 應用的版本名稱和版本號 PackageManager pm = mContext.getPackageManager(); PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES); pw.print("App Version: "); pw.print(pi.versionName); pw.print('_'); pw.println(pi.versionCode); // android版本號 pw.print("OS Version: "); pw.print(Build.VERSION.RELEASE); pw.print("_"); pw.println(Build.VERSION.SDK_INT); // 手機制造商 pw.print("Vendor: "); pw.println(Build.MANUFACTURER); // 手機型號 pw.print("Model: "); pw.println(Build.MODEL); // cpu架構 pw.print("CPU ABI: "); pw.println(Build.CPU_ABI);}/** * 上傳到服務器(這里需要實現) */private void uploadExceptionToServer() {}}
APP(自定義的Application)
public class APP extends Application { //log路徑 private static final String LOG_PATH= Environment .getExternalStorageDirectory().getPath() + File.separator + "Live" + File.separator + "log" + File.separator; @Overridepublic void onCreate() { super.onCreate(); CrashHandler.getInstance().init(this,LOG_PATH);}}
MainActivity
class MainActivity : AppCompatActivity(){var mBtnSecond:Button?=null;override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) checkPermission() initView()}fun initView(){ mBtnSecond=findViewById(R.id.btn_second) mBtnSecond?.setOnClickListener{ var intent= Intent(this,SecondActivity::class.java) startActivity(intent) }}/** * 6.0以下版本(系統自動申請) 不會彈框 * 有些廠商修改了6.0系統申請機制,他們修改成系統自動申請權限了 */private fun checkPermission(){ val permissionItems = ArrayList<PermissionItem>() permissionItems.add(PermissionItem(Manifest.permission.READ_EXTERNAL_STORAGE, "讀取空間", R.drawable.permission_ic_phone)) permissionItems.add(PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE,"存儲空間",R.drawable.permission_ic_storage)) HiPermission.create(this) .title("親愛的上帝") .msg("為了能夠正常使用,請開啟這些權限吧!") .permissions(permissionItems) .style(R.style.PermissionDefaultBlueStyle) .animStyle(R.style.PermissionAnimScale) .checkMutiPermission(object : PermissionCallback { override fun onClose() { Toast.makeText(this@MainActivity,"用戶關閉了權限",Toast.LENGTH_LONG).show(); } override fun onFinish() { Toast.makeText(this@MainActivity,"初始化完畢!",Toast.LENGTH_LONG).show(); } override fun onDeny(permission: String, position: Int) { } override fun onGuarantee(permission: String, position: Int) { } })} }
MainActivity.png
CrashActivity
public class CrashActivity extends AppCompatActivity {Button mBtnCrash;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); mBtnCrash=findViewById(R.id.btn_crash); mBtnCrash.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(CrashActivity.this,"出現異常了",Toast.LENGTH_LONG).show(); throw new RuntimeException(toUtf8("出現異常了")); } });}public static String toUtf8(String str) { String result = null; try { result = new String(str.getBytes("UTF-8"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result;}}
CrashActivity.png
最后
這里需要注意的是,在MainActivity中用的是Kotlin寫的權限控制,也就是運行時權限
implementation 'me.weyye.hipermission:library:1.0.7'
要保存日志當然需要SD卡的讀寫權限。
項目地址:https://gitee.com/1032200695/CrashException
本地下載:CrashException.rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答