0

这是在 SDCard for Android 中获取和转储日志的方法。这工作正常,但我想知道我们是否可以使用过滤器来跟踪与特定标签相关的日志。

我试过logcat -s *TagIWant;了,但没有用。还有什么建议吗?

public static void Log(Context context){
    String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
    String command = "logcat -d *:V";

    Log.d(TAG, "command: " + command);

    try{
        Process process = Runtime.getRuntime().exec(command);

        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        try{
            File file = new File(filename);
            file.createNewFile();
            FileWriter writer = new FileWriter(file);
            while((line = in.readLine()) != null){
                writer.write(line + "\n");
            }
            writer.flush();
            writer.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
    catch(IOException e){
        e.printStackTrace();
    }
}
4

3 回答 3

0

将“Your_Tag”替换为您的标签,这将为您提供带有优先级“调试”及以上标签的日志。

String command = "logcat Your_Tag:D *:S";
Process process = Runtime.getRuntime().exec(command);

检查更多http://developer.android.com/tools/debugging/debugging-log.html

于 2013-11-06T09:42:20.363 回答
0
try {               
        Process process = Runtime.getRuntime().exec("logcat -d");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;

        Pattern pattern = Pattern.compile("XXX", 0);

        while ((line = bufferedReader.readLine()) != null) {
            if (patternu != null
                    && !pattern.matcher(line).find()) {
                continue;
            }
            log.append(line);
            log.append('\n');
        }

    } catch (IOException e) {

    }
于 2013-07-15T06:06:47.073 回答
0

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

  while ((line = bufferedReader.readLine()) != null) {
            if(line.contains("The String you want to exist in your log")){
                writer.write(line + "\n");
            }
            else{continue;}
        }
于 2021-02-07T07:17:54.630 回答