Android log 日志分析


一. Log 日志中 Bug 類型

  1. 程序異常強制關閉: Force Close ,Fatal
  2. 程序無響應: Application Not Response , ANR(應用無響應)。一般是主線程超時無響應造成的。
    • ANR 類型有:
      • keyDispatchTimeout(Activity):5秒無響應

        • 位於 ActivityManagerService.java 類中

          // How long we wait until we timeout on key dispatching.
          

          static final int KEY_DISPATCHING_TIMEOUT = 5*1000;

      • BroadcaseTimeout(Receiver):10秒無響應

      • ServiceTimeout(Service):20秒無響應

    • ANR 產生原因:
      • UI 線程做了耗時操作。比如在 Activity 中做了超過 5 秒無響應的耗時操作,比如文件讀寫,數據庫讀寫,就會出現 ANR
    • ANR 避免:
      • 不要在 UI 線程做耗時操作。

一. log 日志文件的位置

  • 可以通過在 application 中實現接口: UncaughtExceptionHandler, 在實現的 ·uncaughtException()· 方法中把產生的 log 保存到本地。

二. log 日志文件的結構

三. log 日志中 bug 的查找和分析

  1. 日志文件關鍵字

    • Force CloseFatal
    • Application Not Response : ANR
  2. 查看 Log 基本步驟

    1. 如果是 ANR問題,在日志文件中搜索ANR,快速定位到關鍵事件信息。
    2. 如果是Force Close或其他異常退出,搜索FATAL,快速定位到關鍵事件信息。
    3. 定位到關鍵事件信息后,查看信息是否明確,如何不明確的話,查看具體的進程和線程跟蹤的日志,來定位 bug
  3. Force Close 快速定位案例

    • 出現了 Force Close 的情況,搜索關鍵字 FATAL,得到以下 Log

        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: FATAL EXCEPTION: main
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: Process: cc.lijingbo.crashhandler, PID: 16078
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: 	at cc.lijingbo.crashhandler.MainActivity$1.onClick(MainActivity.java:23)
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: 	at android.view.View.performClick(View.java:5232)
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: 	at android.view.View$PerformClick.run(View.java:21289)
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: 	at android.os.Handler.handleCallback(Handler.java:739)
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:95)
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:168)
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:5885)
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
      
        	10-18 14:36:35.197 16078 16078 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
      
        	10-18 14:36:35.207  1187 17570 E ActivityManager: App crashed! Process: cc.lijingbo.crashhandler
      
        	10-18 14:36:35.207  1187 17570 W ActivityManager:   Force finishing activity cc.lijingbo.crashhandler/.MainActivity
      
    • 看到因為 NullPointerException 造成的。
      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference

    • 然后下一句提示項目的代碼出錯的地方: at cc.lijingbo.crashhandler.MainActivity$1.onClick(MainActivity.java:23),在 MainActivityonClick 方法的第23

    • 源碼
      public class MainActivity extends AppCompatActivity {

        	private TextView tv1;
        	private TextView tv2;
      
        	@Override
        	protected void onCreate(Bundle savedInstanceState) {
        		super.onCreate(savedInstanceState);
        		setContentView(R.layout.activity_main);
        		tv1 = (TextView) findViewById(R.id.tv1);
        		tv1.setOnClickListener(new OnClickListener() {
        			@Override
        			public void onClick(View v) {
        				Toast.makeText(MainActivity.this, tv2.getText().toString(), Toast.LENGTH_SHORT).show();
        			}
        		});
        	}
        }
      
    • 可以看到是因為使用 Toast 的時候,tv2 沒有初始化,造成的空指針,把 tv2 初始化就可以了。

  4. ANR 快速定位案例

四. adb logcat 的學習和使用

  • 抓取設備中日志到指定目錄
    • adb logcat >crash.log

參考:
Logcat 官網語法: https://developer.android.google.cn/studio/command-line/logcat.html#Syntax
Logcat 的使用:https://developer.android.google.cn/studio/debug/am-logcat.html
Analyze a Stack Trace: https://developer.android.google.cn/studio/debug/stacktraces.html
studio debug: https://developer.android.google.cn/studio/debug/index.html
如何分析解決 ANR : http://blog.csdn.net/dadoneo/article/details/8270107


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM