Android開發中日誌錯誤資訊的獲取與上報
阿新 • • 發佈:2019-02-02
1、背景介紹
在做Android開發過程中,開發階段,我們可以通過DDMS看到輸出的日誌資訊,或者是異常報錯,這個時候一般都是執行時一場,比如空指標,記憶體溢位等等問題,我們在開始做開發的時候就可以得到這些資訊。但是當我們的應用釋出之後呢,對於不同的一些機型,我們可能有不同的錯誤型別,這個時候,我們無法得知應用程式的報錯資訊,就需要在客戶端收集起來,回傳到伺服器。
2、異常捕獲
Android中提供了一個全域性異常的捕獲,要點是:
1)實現UncaughtExceptionHandler,在方法uncaughtException中處理沒有捕獲的異常。
2)繼承Application ,在其中呼叫Thread方法setDefaultUncaughtExceptionHandler,來捕獲異常
別的不多說,程式碼如下:
/** * Created by ljtyzhr on 2015/4/16. */ public class MyApplication extends Application { public void onCreate(){ super.onCreate(); GlobalException handler = GlobalException.getInstance(); Thread.setDefaultUncaughtExceptionHandler(handler); } } /** * 構造方法私有化,獲取得到一個單例 * Created by ljtyzhr on 2015/4/16. */ public class GlobalException implements UncaughtExceptionHandler{ private final static GlobalException myCrashHandler = new GlobalException(); private GlobalException(){ } public static synchronized GlobalException getInstance(){ return myCrashHandler; } public void uncaughtException(Thread arg0, Throwable arg1){ // 在這裡捕獲異常資訊 } }
3、日誌資訊
我們可以通過命令列獲取日誌資訊,當日志資訊檔案,到達一定的大小之後,就可以回傳到伺服器,供開發人員做分析使用。程式碼如下:
import android.content.Context; import android.os.Environment; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; /** * Created by ljtyzhr on 2015/4/16. */ public class LogcatHelper { private static LogcatHelper INSTANCE = null; private static String PATH_LOGCAT; private LogDumper mLogDumper = null; private int mPId; /** * * 初始化目錄 * * */ public void init(Context context) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) {// 優先儲存到SD卡中 PATH_LOGCAT = Environment.getExternalStorageDirectory() .getAbsolutePath() + File.separator + "LogcatInfos"; } else {// 如果SD卡不存在,就儲存到本應用的目錄下 PATH_LOGCAT = context.getFilesDir().getAbsolutePath() + File.separator + "LogcatInfos"; } File file = new File(PATH_LOGCAT); if (!file.exists()) { file.mkdirs(); } } public static LogcatHelper getInstance(Context context) { if (INSTANCE == null) { INSTANCE = new LogcatHelper(context); } return INSTANCE; } private LogcatHelper(Context context) { init(context); mPId = android.os.Process.myPid(); } public void start() { if (mLogDumper == null) mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT); mLogDumper.start(); } public void stop() { if (mLogDumper != null) { mLogDumper.stopLogs(); mLogDumper = null; } } private class LogDumper extends Thread { private Process logcatProc; private BufferedReader mReader = null; private boolean mRunning = true; String cmds = null; private String mPID; private FileOutputStream out = null; public LogDumper(String pid, String dir) { mPID = pid; try { out = new FileOutputStream(new File(dir, "Log-" + MyDate.getFileName() + ".log")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } /** * * 日誌等級:*:v , *:d , *:w , *:e , *:f , *:s * * 顯示當前mPID程式的 E和W等級的日誌. * * */ // cmds = "logcat *:e *:w | grep \"(" + mPID + ")\""; // cmds = "logcat | grep \"(" + mPID + ")\"";//列印所有日誌資訊 cmds = "logcat -s test";//列印標籤過濾資訊 // cmds = "logcat *:e *:i | grep \"(" + mPID + ")\""; } public void stopLogs() { mRunning = false; } @Override public void run() { try { logcatProc = Runtime.getRuntime().exec(cmds); mReader = new BufferedReader(new InputStreamReader( logcatProc.getInputStream()), 1024); String line = null; while (mRunning && (line = mReader.readLine()) != null) { if (!mRunning) { break; } if (line.length() == 0) { continue; } if (out != null && line.contains(mPID)) { out.write((MyDate.getDateEN() + " " + line + "\n") .getBytes()); } } } catch (IOException e) { e.printStackTrace(); } finally { if (logcatProc != null) { logcatProc.destroy(); logcatProc = null; } if (mReader != null) { try { mReader.close(); mReader = null; } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } out = null; } } } } }