8

I've read this article already. But it seems most of the text it shows are not in my application. How can I filter the message and let it only shows the log for my application. In another word, I want to show it like the one in Android Studio (only showing the error log from my app, showing time stamp,etc):

I tried something like "logcat -d -v time", but doesn't work. Any idea? Thanks.

enter image description here

1
  • 1
    You can always parse the lines from logcat yourself and ignore any tag you don't want. Commented Jan 15, 2015 at 5:45

1 Answer 1

18

Try following code to get logs of your application only.

public final class MyAppLogReader {

    private static final String TAG = MyAppLogReader.class.getCanonicalName();
    private static final String processId = Integer.toString(android.os.Process
            .myPid());

    public static StringBuilder getLog() {

        StringBuilder builder = new StringBuilder();

        try {
            String[] command = new String[] { "logcat", "-d", "-v", "threadtime" };

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

            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(processId)) {
                    builder.append(line);
                    //Code here
                }
            }
        } catch (IOException ex) {
            Log.e(TAG, "getLog failed", ex);
        }

        return builder;
    }
}

Edit

Add below permission to your AndroidManifest file

<uses-permission android:name="android.permission.READ_LOGS" />
Sign up to request clarification or add additional context in comments.

4 Comments

I edited your post by adding -d command line parameter to logcat, else the function never returns, just waits and adds more log lines as they are posted. Also under newer versions of Android (I think from Jellybean up) and without root access, only the logcat entries generated by your own app package are included.
Don't forget to add '<uses-permission android:name="android.permission.READ_LOGS" />' in your manifest for this to work.
READ_LOGS added in API level 1 String READ_LOGS Allows an application to read the low-level system log files. Not for use by third-party applications, because Log entries can contain the user's private information. developer.android.com/reference/android/…
is it possible to read logs of other application using package name ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.