How to collect LogCat messages in an Android application

好久不见. 提交于 2019-12-04 17:22:34

Try this: Note that in Android 4 you will only see the log messages that were written by your own app unless you 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;
    }
}
David S.

Why not just write them to a file instead? LogCat is really for real-time logs. There are lots of good quality logging packages that can log to a file if that's what you want to do.

Just as an example:

How to write logs in text file when using java.util.logging.Logger

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!