【问题标题】:Android: Filter Logcat by tag using a regexAndroid:使用正则表达式按标签过滤 Logcat
【发布时间】:2012-04-04 23:53:15
【问题描述】:

在记录我的标签时,总是如下所示:

com.myapp.SomeActivity

对于每个日志语句,我在第一部分前面加上“com.myapp”。到进行日志调用的类的标签。例如:

MyActivity extends Activity{

private static final String TAG = "MyActivity";

 public void someMethod(){
  LogWrapper.e(TAG, "Something is wrong here.");
 } 
}

我的类 LogWrapper 基本上做以下事情

public class LogWrapper {
 private static final String applicationTAG = "com.myapp.";

 public static void e(String classTag, String message){
  Log.e(applicationTAG + classTag, message);
 }
}

所以我所做的就是在类的标签前添加一个静态标签。原因是,我想通过我的应用程序写入的 Log 语句过滤 logcat 输出,并避免将来自系统或其他应用程序的日志弄乱我的日志文件。

因此我想出了这样获取日志:

String command = "logcat -d -v time com.myapp.*:I *:S"
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);
 log.append(StringUtils.NEW_LINE);
}

但是,我最终得到了一个空的日志文件。这让我觉得正则表达式“com.myapp.:I”不起作用。我也尝试省略星号(),但这都不起作用。但是,如果我使用 "*:I" 就可以了。

这让我想知道,是否只能通过完整的标签名称进行过滤?有没有办法可以获取由“com.myapp”过滤的日志。 ?

我猜 B 计划 将是在 while 循环中我可以检查该行是否包含我的标签并仅在这种情况下附加它,但我真的很想避免这种情况.. .

【问题讨论】:

    标签: android filter logcat


    【解决方案1】:

    为了解决这个问题,我放弃了我在第一篇文章中暗示的 B 计划更优雅(但仍然未知)的解决方案。我使用以下正则表达式过滤使用命令 logcat -d -v time *:I 获得的日志文件的每一行(使用 Runtime.getRuntime().exec(command) 执行):

    .*([DIWE]{1}/com.myapp.).*
    

    上面的正则表达式匹配这样的日志语句:

    I/com.myapp.MyClass Crashed and did not recover - too bad.
    

    [DEWI] 表示,日志级别必须是 D(ebug)、E(rror)、W(arning) 或 I(nfo),以便正则表达式匹配。如果你也想提取 V(erbose) 日志,只需添加一个 "V" 。

    【讨论】:

      猜你喜欢
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 2015-01-05
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多