How to collect LogCat messages in an Android application

I have an application, and I would like to collect LogCat messages of the specified level and tag.

Can I somehow receive accumulated messages at some point? I do not want to collect messages one by one, it should be their sum, for example, when I use adb to read a real log. Is it possible?

0
source share
2 answers

Try this: note that in Android 4 you will only see log messages that were written by your own application if you do not have root access.

public static String getLog(Context c) {
    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);
            log.append("\n");
        }

       return log.toString();

    } catch (IOException e) {
       return null;
    }
}
+4
source

? LogCat . , , , .

:

java.util.logging.Logger

+3