【问题标题】:android how to capture only my log entries in the application programmaticallyandroid如何以编程方式仅捕获应用程序中的日志条目
【发布时间】:2017-08-08 20:06:26
【问题描述】:

我想单独获取我放在应用程序中的日志条目并将其显示在可滚动的文本视图中。例如

Log.e(SAMPLE_ACTIVITY_TAG, "App started") 

我只需要那些错误日志,我不想要其他不需要的日志。 logcat命令行工具可以吗?我已经尝试了下面的代码,但我得到了完整的应用程序设备日志

 Process process = Runtime.getRuntime().exec("logcat -d");
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

        StringBuilder log=new StringBuilder();
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            log.append(line +"\n");
        }
        textView.setText(log.toString());
        textView.setMovementMethod(new ScrollingMovementMethod());

任何建议

【问题讨论】:

    标签: android android-logcat android-log


    【解决方案1】:

    请使用以下给定命令创建进程对象:

    logcat --pid=PID -d

    您需要将 PID 替换为您的应用程序进程 ID。可以使用以下代码行获取进程ID

    int pid = android.os.Process.myPid();
    

    您的最终源代码应该是这样的。请试一试

            int pid = android.os.Process.myPid();
            String command = "logcat --pid="+pid+" -d";
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
    
            StringBuilder log=new StringBuilder();
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                log.append(line +"\n");
            }
            textView.setText(log.toString());
            textView.setMovementMethod(new ScrollingMovementMethod());
    

    【讨论】:

      猜你喜欢
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多