zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    3.3 React Hooks
    ES6高阶函数
    ES6扩展运算符
    4.0不用npm,cnpm。使用yarn包启动react项目
    4.3 webpack打包学习
    4.2 Node.js模块化教程
    4.1React模块化
    vue组件化开发
    js箭头函数
    weblogic_exploit
  • 原文地址:https://www.cnblogs.com/liyiran/p/7686296.html
Copyright © 2011-2022 走看看