读过滤日志cat(以编程方式)?

时间:2012-02-07 10:59:37

标签: android logcat log-level

如何阅读Level警告的过滤日志?

这就是我所知道的......

logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d"});

br = new BufferedReader(new InputStreamReader(logcat.getInputStream()),4*1024);
String line;
  final StringBuilder log = new StringBuilder();
  String separator = System.getProperty("line.separator"); 
    while ((line = br.readLine()) != null)
     {
       log.append(line);
         log.append(separator);
}

4 个答案:

答案 0 :(得分:1)

-s标志允许您根据标记和级别e,w,d,v过滤logcat输出,以便您可以尝试修改您的行。

logcat = Runtime.getRuntime().exec(new String[]{"logcat", "-d","-s","*:w"});

答案 1 :(得分:0)

if (line.contains("WARN")) {
// ...
}

答案 2 :(得分:0)

您可以像下面的代码一样轻松过滤要编写的行:

  while ((line = bufferedReader.readLine()) != null) {
            if(line.contains("OkHttp")){
            log.append(line.substring(line.indexOf("OkHttp")));
            log.append('\n');}
            else{continue;}
        }

答案 3 :(得分:-1)

 try {
                  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);
                  }
                  TextView tv = (TextView)findViewById(R.id.textView1);
                  tv.setText(log.toString());
                } catch (IOException e) {
                }

希望你得到你想要的......