How to read Logcat contents programmatically in Android?

This is an example to read Logcat contents programmatically in Android.
Normal developers won’t need this, but still it is a good thing to know how to do it.

package pack.coderzheaven;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ReadLogDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	 super.onCreate(savedInstanceState);
    	 setContentView(R.layout.main);
    	    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);
    	      }
    	      TextView tv = (TextView)findViewById(R.id.textView1);
    	      tv.setText(log.toString());
    	    } catch (IOException e) {
    	    }
    	  }
}
Read Logcat Demo

Read Logcat Demo

if you like the post then click on the plus button to share it with your friends and leave your valuable comments.

6 thoughts on “How to read Logcat contents programmatically in Android?

  1. murali

    I have run the sample application. it runs successfully. but i use “locat -e” option instead of “logcat -d”. it does not print anything in the text view. i requested you to add the solution for it.

    Thank you.

    Reply
  2. Tauon

    Unfortunately, a cut and paste of this code does not work. The very first readLine() returns null and so it drops straight out the loop. I am guessing that because Murali got it working, there is something that needs to be done that has not been described in this post?

    Reply
  3. Kontakthi

    Sorry,a bit late but I resolved the null return from the readLine() call by adding the following line to my manifest file:

    Reply
    1. Arun Pandian

      Hi,
      If u r wondering, set your permission in android manifest xml page to

      ..

      Please let me your solutions

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>